Wednesday, February 3, 2010
Serialize & Deserialize + C#
Serialize (convert an object instance to an XML document):
// A
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):
//A
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:
MyCla
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment