Here's a useful trick for you; put your desired xml in a file,
test.xml.
Now load the VS command prompt (in the start menu) and execute (note
you might need to specify the path):
===
xsd test.xml
xsd test.xsd /classes
===
The first line creates test.xsd from your test.xml; the second line
creates test.cs from your test.xsd; this gives you an example of how
you can generate the desired output with attributes (heck, you could
just use this test.cs "as is") - for example:
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Identity {
get {
return this.identityField;
}
set {
this.identityField = value;
}
}
If you want to change the markup at the start of the xml (or the
spacing etc), you can do this via XmlWriterSettings - for example:
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using(XmlWriter writer = XmlWriter.Create(path, settings))
{
serializer.Serialize(writer, obj);
writer.Close();
}
Marc
christery@gmail.com - 29 Jan 2008 17:58 GMT
> Here's a useful trick for you; put your desired xml in a file,
> test.xml.
[quoted text clipped - 6 lines]
> xsd test.xsd /classes
> ===
OK, I just serialized the instance of the class into xml something
like this example:
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream ( );
XmlSerializer xs = new XmlSerializer ( typeof
( Animal ) );
XmlTextWriter xmlTextWriter = new XmlTextWriter
( memoryStream, Encoding.UTF8 );
xs.Serialize ( xmlTextWriter, pObject );
memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString
( memoryStream.ToArray ( ) );
But I had to loose the double UTF8 encoding... got some strange char
in the beginning..
This just get pObject and write it to a memorystream but i lose a bit
of control, its just the instance of the class that is XML coded
Your way might be better, then I have control over produced code via
the definition xsd
//CY