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?
我尝试使用xsd.exe将.xsd文件转换为C#类。它工作,但我仍然不太确定如何使用这个类。它有几个类级属性,但最有趣的是System.Xml.Serialization.XmlTypeAttribute。哪个类使用该属性?
Is there a simple way to turn an instantiation of this class into a string of XML?
有没有一种简单的方法可以将此类的实例化转换为XML字符串?
3 个解决方案
#1
6
Super straight-forward. I love the xsd tool. I have taken some liberties below.
超级直截了当。我喜欢xsd工具。我在下面采取了一些*。
//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
0
Yes. Look at XmlSerializer [and StringWriter if you like].
是。看看XmlSerializer [和StringWriter,如果你愿意]。
#3
0
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.
使用像普通类这样的类。然后,当您将它们序列化为XML时,XML将根据模式进行验证。您还可以使用对模式进行验证的XML,并将其反序列化为类的实例。
#1
6
Super straight-forward. I love the xsd tool. I have taken some liberties below.
超级直截了当。我喜欢xsd工具。我在下面采取了一些*。
//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
0
Yes. Look at XmlSerializer [and StringWriter if you like].
是。看看XmlSerializer [和StringWriter,如果你愿意]。
#3
0
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.
使用像普通类这样的类。然后,当您将它们序列化为XML时,XML将根据模式进行验证。您还可以使用对模式进行验证的XML,并将其反序列化为类的实例。