I created an XSD file from Visual Studio and can generate a sample XML as well, but my goal is to use this XSD to create an XML file at runtime.
I used XSD.exe to generate a class from my XSD file and then created a program to populate the object from the "class". How can I serialize the object to an XML file?
Both those examples leave the stream open, and XmlFormatter is part of the BizTalk libs - so XmlSerializer would be more appropriate:
using (Stream stream = File.Open(fileName, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
serializer.Serialize(stream, MyObject);
stream.Flush();
}
When you have created classes to serialize and deserialize the Xml file using the XSD.exe tool you can write your instances back to files using ..
Serialization! (Archive)
Stream stream = File.Open(filename, FileMode.Create);
XmlFormatter formatter = new XmlFormatter (typeof(XmlObjectToSerialize));
formatter.Serialize(stream, xmlObjectToSerialize);
stream.Flush();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With