To use the XML Serializer in C#.NET,
use as follows
public static class SerializationHelper { ////// Serializes the object and returns the serialized object's xml string /// /// The obj. ///public static string SerializeObject(Object obj) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); xmlSerializer.Serialize(memoryStream, obj); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return new System.IO.StreamReader(memoryStream).ReadToEnd(); } /// /// Desserializeobjs the specified object XML string. /// ////// The object XML string. /// public static object DeSerializeObject (string objectXmlString) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { using (System.IO.StreamWriter swr = new System.IO.StreamWriter(memoryStream)) { swr.Write(objectXmlString); swr.Flush(); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return (T)xmlSerializer.Deserialize(memoryStream); } } } }
Comments
Post a Comment