1

我使用 Visual Studio xsd 工具生成了一个带有 XML 架构 (.xsd) 的类。现在我有了这个类,我想将该对象输出回 XML,如我的 xsd 定义的那样。我想知道该怎么做。谢谢!

4

1 回答 1

2

您需要负责XmlSerializer序列化您的课程:

using System.Text; // needed to specify output file encoding
using System.Xml;
using System.Xml.Serialization; // XmlSerializer lives here

// instance of your generated class
YourClass c = new YourClass();

// wrap XmlTextWriter into a using block because it supports IDisposable
using (XmlTextWriter tw = new XmlTextWriter(@"C:\MyClass.xml", Encoding.UTF8))
{
    // create an XmlSerializer for your class type
    XmlSerializer xs = new XmlSerializer(typeof(YourClass));

    xs.Serialize(tw, c);
}
于 2012-07-01T07:37:28.257 回答