Using XmlSerializer for Serialization/Deserialization

This is something I wrote a while ago to allow easy way to serialize/deserialize various objects to and from XML by creating an adapter around the XmlSerializer class.
Since I don’t know how this XML is going to be used this class stores it inside a string.

Serialize usage:
string s = SimpleSerializer.Instance.ToString(myObject);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(s);

Deserialize:
MyClass obj = SimpleSerializer.Instance.FromString(xDoc.OuterXML);

    /// <summary>    /// Objects serializer/deserializer    /// </summary>    public class SimpleSerializer    {        private static readonly SimpleSerializer instance = new SimpleSerializer(); //Singleton

        /// <summary>        /// Gets the class's instance.        /// </summary>        /// <value>The instance.</value>        public static SimpleSerializer Instance        {            get { return instance; }        }

        /// <summary>        /// Private constructor - prevents tempering with the singleton pattern        /// </summary>        private SimpleSerializer()        {        }

        static SimpleSerializer()        {        }

        /// <summary>        /// Converts an object to it's XML represntation        /// </summary>        /// <param name="message">The source object</param>        /// <returns>XML represntation</returns>        public string ToString(object message)        {            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(message.GetType());            using (System.IO.StringWriter sw = new System.IO.StringWriter())            {                serializer.Serialize(sw, message);                return sw.ToString();            }        }

        /// <summary>        /// Converts an XML to an object        /// </summary>        /// <param name="message">The XML</param>        /// <param name="type">The expected result class</param>        /// <returns>A new object deserialized from the XML</returns>        public object FromString(string message, Type type)        {            using (System.IO.StringReader sr = new System.IO.StringReader(message))            {                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);                return serializer.Deserialize(sr);            }        }

        /// <summary>        /// Converts an XML to an object        /// </summary>        /// <param name="message">The XML</param>        /// <typeparam name="T">The expected result class</typeparam>        /// <returns>A new object deserialized from the XML</returns>        public T FromString<T>(string message) where T: new()        {            using (System.IO.StringReader sr = new System.IO.StringReader(message))            {                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));                return (T)serializer.Deserialize(sr);            }        }

    } 

Category: Useful .Net classes, WCF


Leave a Reply