How to use xsd.exe /c output

喜你入骨 提交于 2019-12-12 07:35:06

问题


I tried using xsd.exe to convert an .xsd file into a C# class. 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. Which class uses that attribute?

Is there a simple way to turn an instantiation of this class into a string of XML?


回答1:


Super straight-forward. I love the xsd tool. 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);



回答2:


Yes. Look at XmlSerializer [and StringWriter if you like].




回答3:


Use the classes like normal classes. Then, when you serialize them to XML, the XML will validate against the schema. You can also take XML that validates against the schema and deserialize it back into instances of the classes.



来源:https://stackoverflow.com/questions/2232182/how-to-use-xsd-exe-c-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!