views:

148

answers:

3

I like to create a xml file with following structure...

<Disposition>
      <DispositionTextList Description="">
          <DispositionText value="">
          <DispositionText value="">
      </DispositionTextList>
      <DispositionTextList Description="">
          <DispositionText value="">
          <DispositionText value="">
      </DispositionTextList>
</Disposition>

Can any one give me the such a class using that i can serialize/deserialize?

+5  A: 
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.

Braveyard
Thanks, But i need a class to manage that xml file to read/write the content.
Sarathi1904
@Aaron +1 - beat me to it
ChrisBD
@Sarathi1904, Please read this article, actually it is using an old school technique but I believe it will help you : http://tinyurl.com/b4eff
Braveyard
But it's the best and clear look of an xml file, I would recommend you to use XDocument, rather than XmlDocument if you understand what I mean.
Braveyard
+1  A: 

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.

Murph
A: 

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.

Eric Anastas