简体   繁体   English

如何使用xsd.exe / c输出

[英]How to use xsd.exe /c output

I tried using xsd.exe to convert an .xsd file into a C# class. 我尝试使用xsd.exe将.xsd文件转换为C#类。 It worked, but I'm still not quite sure how to use this class. 它工作,但我仍然不太确定如何使用这个类。 It has several class-level attributes but the most interesting is System.Xml.Serialization.XmlTypeAttribute. 它有几个类级属性,但最有趣的是System.Xml.Serialization.XmlTypeAttribute。 Which class uses that attribute? 哪个类使用该属性?

Is there a simple way to turn an instantiation of this class into a string of XML? 有没有一种简单的方法可以将此类的实例化转换为XML字符串?

Super straight-forward. 超级直截了当。 I love the xsd tool. 我喜欢xsd工具。 I have taken some liberties below. 我在下面采取了一些自由。

//From xml to object
YourRootType inst = new XmlSerializer(typeof(YourRootType)).Deserialize(XmlReader.Create("some.xml"));

//From object to xml
Using(FileStream fs = new FileStream("some.xml", FileMode.Create))
   new XmlSerializer(typeof(YourRootType)).Serialize(fs, inst);

Yes. 是。 Look at XmlSerializer [and StringWriter if you like]. 如果你愿意,请查看XmlSerializer [和StringWriter]。

Use the classes like normal classes. 使用像普通类这样的类。 Then, when you serialize them to XML, the XML will validate against the schema. 然后,当您将它们序列化为XML时,XML将根据模式进行验证。 You can also take XML that validates against the schema and deserialize it back into instances of the classes. 您还可以获取针对模式验证的XML,并将其反序列化为类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM