Geeks With Blogs
Sharvan Dhaka blog
Serialize (convert an object instance to an XML document):

// Assuming obj is an instance of an object
XmlSerializer ser = new XmlSerializer(obj.GetType());
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, obj);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());

Deserialize (convert an XML document into an object instance):

//Assuming doc is an XML document containing a serialized object and objType is a System.Type set to the type of the object.
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new XmlSerializer(objType);
object obj = ser.Deserialize(reader);
// Then you just need to cast obj into whatever type it is eg:
MyClass myObj = (MyClass)obj;
Posted on Sunday, October 15, 2006 5:43 PM Asp.net , c# , Tricky Solution/ Code | Back to top


Comments on this post: Serialize & Deserialize c# asp.net

# re: Serialize & Deserialize
Requesting Gravatar...
Really Nice
Left by Jitendra on Oct 15, 2006 6:46 PM

# re: Serialize & Deserialize c# asp.net
Requesting Gravatar...
it was useful
Left by AMuthan on May 30, 2011 6:27 AM

Your comment:
 (will show your gravatar)


Copyright © Sharvan Dhaka | Powered by: GeeksWithBlogs.net