Skip to content

Object from/to XML

George George

Asked May 7, 2008, 5:02 AM EDT4 answers
Share
LinkedInX

Hello everyone,


I want to convert an object instance to XML string, and later read the XML string to construct the object again. Any samples?

(I find some serialization samples, but all about binary format, I prefer to use XML format.)


thanks in advance,
George

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    0

    Do you have a class object with members or talking about any object?

    In general, a DataSet is an object and you can read an XML in a DataSet using ReadXml method and write DataSet to XML doc by using DataSet.WriteXml method.

    0
  • 0

    Sorry, my English is not very good. I do not have any database objects, like DataSet. I mean general C# object -- class instance. Any ideas?

     

    regards,

    George

    0
  • 0

    You can serialize an object to xml using XmlSerializer class

       System.Xml.Serialization.XmlSerializer xS = new System.Xml.Serialization.XmlSerializer(myObj.GetType());    // myObj is your object :)
          xS.Serialize(Console.Out, myObj);

    0
  • 0

    Thanks Awadhesh,

     

    This is what I need. :-)

     

    regards,

    George

    0