开发者

How to serialize a class generated from XSD to XML

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 开发者_如何学Go"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();


Binary format is binary, use the XML version for XML:

XmlFormatter serializer = new XmlFormatter(typeof(MyObject));
serializer.Serialize(stream, object1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜