Creating XML file with Serialization
I want to create an XML file with th开发者_如何学编程e following structure. Can any one give me a class to use that can serialize/deserialize this XML?
<Disposition>
<DispositionTextList Description="">
<DispositionText value="">
<DispositionText value="">
</DispositionTextList>
<DispositionTextList Description="">
<DispositionText value="">
<DispositionText value="">
</DispositionTextList>
</Disposition>
XElement element = new XElement("Disposition",new XElement("DispositionTextList",
new XAttribute("Description",""),new XElement("DispositionText",
new XAttribute("value","")),XElement("DispositionText",
new XAttribute("value",""))),new XElement("DispositionTextList",
new XAttribute("Description",""),new XElement("DispositionText",
new XAttribute("value","")),XElement("DispositionText",new XAttribute("value",""))))
You can use XDocument API in C#, it is also enumerable which means you can enumerate with LINQ to access and manipulate the xml file.
so to save it into a file :
element.Save("path");
or you can even use other overloads to use text writer or output stream.
By far the best way to learn about this is to go play - the built in serialization methods are well documented in the MSDN library (which is online) including examples that should be more than adequate for the above level of complexity.
Your life may be somewhat complicated by the use of attributes in the XML but you can add annotations to your class to control the way that elements are output
In fact the link that Aaron has provided is the starting point you need.
Do you need the data in that format specifically, or do you only need your data in XML. I agree with Murph though. I'd look into the built-in XML Serialization functions.
Here's a good tutorial on serialization
http://www.diranieh.com/NETSerialization/XMLSerialization.htm
So basically you create a class with public properties for the data you are trying store. Then you create a instance of XMLSerializer based on your class and use it to serialize any number of instances of your class to an XML file.
精彩评论