Here is a code snippet that I continuously use; it is used to serialize an object to a string and deserialize an object from a string. (The interesting thing to see here is during serialization the code uses a StringBuilder object which is passed to an XMLWriter which is in turn sent to the XMLSerializer).
public static object Deserialize(string data, Type dataType) { using (TextReader rd = new StringReader(data)) { XmlSerializer sr = new XmlSerializer(dataType); return sr.Deserialize(rd); } } public static string Serialize(object item) { StringBuilder bld = new StringBuilder(); using (XmlWriter xmlWr = XmlWriter.Create(bld)) { XmlSerializer sr = new XmlSerializer(item.GetType()); sr.Serialize(xmlWr, item); return bld.ToString(); } }
1 comment:
The formatting of this entry is broken in Firefox. The "Here is a code snippet..." line continues underneath the right sidebar and off the page. Also, with a quick view at the source for this entry, the local style blocks are invalid, with line breaks *within* the style block converted into html breaks.
Post a Comment