Serializing & Deserializing a object

Convert Object to String

Class c1 = new Class();
c1.Name = "Chemistry";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Class));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, c1);
string serializedXML = stringWriter.ToString();

Convert Object to XMLDocument

            Class c1 = new Class();
            c1.Name = "Chemistry";
 
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Class));
            MemoryStream memStream = new MemoryStream();
            StreamWriter streamWriter = new StreamWriter(memStream);
            xmlSerializer.Serialize(streamWriter, c1);
            memStream.Position = 0;
            StreamReader streamReader = new StreamReader(memStream);
            XmlDocument serializedXML = new XmlDocument();
            serializedXML.Load(streamReader);

Convert String to Object

Class c1 = new Class();
XmlSerializer xmlSerializer = new XmlSerializer(c1.GetType());
StringReader stringReader = new StringReader(strValue);
c1= xmlSerializer.Deserialize(stringReader);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s