4 Oct 2007

XMLSerializer: XML Serialization Helper Class

Object Serialization is a process through which an object's state is transformed into some serial data format, such as XML or a binary format.

The purpose of XMLSerializer helper class is to simplify the basic serialization tasks, such as the conversion of objects to XML - string or file - and vice versa (deserialization):

using System;
using System.IO;
using System.Xml;
using SysXmlSer = System.Xml.Serialization;

namespace IFXplus.Serialization
{
    public static class XmlSerializer
    {
        public static string Serialize(Object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj", "Object cannot be null");
            StringWriter sw = new StringWriter();
            string xml;
            SysXmlSer.XmlSerializer xs;
            try
            {
                xs = new SysXmlSer.XmlSerializer(obj.GetType());
                xs.Serialize(sw, obj);
                xml = sw.ToString();
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
            return xml;
        }

        public static T Deserialize<T>(string xml)
        {
            T obj;
            XmlTextReader xtr = null;
            try
            {
                xtr = new XmlTextReader(new StringReader(xml));
                SysXmlSer.XmlSerializer xs = new SysXmlSer.XmlSerializer(typeof(T));
                if (!xs.CanDeserialize(xtr))
                    throw new XmlException("Xml is not deserializable to " + typeof(T));
                obj = (T)xs.Deserialize(xtr);
            }
            finally
            {
                if (xtr != null)
                    xtr.Close();
            }
            return obj;
        }

        public static void SerializeToFile(Object obj, string filePath)
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(filePath);
                SysXmlSer.XmlSerializer xs = new SysXmlSer.XmlSerializer(obj.GetType());
                xs.Serialize(sw, obj);
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }
        }

        public static T DeserializeFromFile<T>(string filePath)
        {
            T obj;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filePath);
                SysXmlSer.XmlSerializer xs = new SysXmlSer.XmlSerializer(typeof(T));
                obj = (T)xs.Deserialize(sr);
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return obj;
        }
    }
}

XMLSerializer Usage Example:

// Serialization:
MySerializableObject obj = new MySerializableObject();
string xml = IFXplus.Serialization.XmlSerializer.Serialize(obj);

// Deserialization:
MySerializableObject obj2 = IFXplus.Serialization.XmlSerializer.Deserialize<MySerializableObject>(xml);

Download

Source and demo: XmlSerializerDemo.zip (3.78 kb)

Currently rated 4.0 by 4 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

trackback

DotNetKicks.com

October 4 2007 10:53

Trackback from DotNetKicks.com

XMLSerializer: XML Serialization Helper Class

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

Gravatar

May 16 2008 16:21