Wednesday, 15 June 2011

Serialization


The process of serialization converts an object into a form so that it can be transported across the network or can be placed in a storage location. The storage location could be a physical file, database or ASP.NET Cache. The state of the Object is preserved in form so that it could be constructed again at some later stage, which is known as Deserialization.
In simple words, we can say an object conversion in text stream is serialization and text stream conversion in object of a class is called deserialization.
There are three formats of serialization:
  1. Binary Serialization - Light and compact used in Remoting.
  2. SOAP Serialization - interoperable use SOAP and used in web Services.
  3. XML Serialization - Custom Serialization.
XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output.
To understand this, we assume one example of Category and Items. We have two classes. One is Category and another is Items. Category has CategoryID, Category Name and Array of Item. While Item has Item ID, Item Name, Item Price and Item Quantity in Stock.
public class Category
{
    private int _CatID;
    private string _CatName;
    private Item[] _Item;

    public int CateboryID
    {
        get
        {
            return _CatID;
        }
        set
        {
            _CatID = value;
        }
    }

    public string CategoryName
    {
        get
        {
            return _CatName;
        }
        set
        {
            _CatName = value;
        }
    }


    public Item[] Item
    {
        get
        {
            return _Item;
        }
        set
        {
            _Item = value;
        }
    }
}

public class Item
{
    private int _ItemID;
    private string _ItemName;
    private int _ItemPrice;
    private int _ItemQtyInStock;

    public int ItemID
    {
        get
        {
            return _ItemID;
        }
        set
        {
            _ItemID = value;
        }
    }

    public string ItemName
    {
        get
        {
            return _ItemName;
        }
        set
        {
            _ItemName = value;
        }
    }

    public int ItemPrice
    {
        get
        {
            return _ItemPrice;
        }
        set
        {
            _ItemPrice = value;
        }
    }

    public int ItemQtyInStock
    {
        get
        {
            return _ItemQtyInStock;
        }
        set
        {
            _ItemQtyInStock = value;
        }
    }
}

We have serialization function which is called by our click event or whenever this is required. In serialization, we have created one object of Category class and another is Item. Category holds one test category “Phone” and then it creates an array of Item class and stores to Category.Item.
using System.Xml.Serialization;
 
private void Serialization()
    {
        Category Cat = new Category();
        Cat.CateboryID = 1;
        Cat.CategoryName = "Phone";
        Item[] Itm = new Item[5];
        for (int i = 0; i < 5; i++)
        {
            Itm[i] = new Item();
            Itm[i].ItemID = i;
            Itm[i].ItemPrice = i * 10;
            Itm[i].ItemQtyInStock = i + 10;
            Itm[i].ItemName = " Item Name : " + i.ToString();
 
        }
        Cat.Item = Itm;
        XmlSerializer ser = new XmlSerializer(Cat.GetType());
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter writer = new System.IO.StringWriter(sb);
        ser.Serialize(writer, Cat);   // Here Classes are converted to XML String. 
                               // This can be viewed in SB or writer.
        // Above XML in SB can be loaded in XmlDocument object
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(sb.ToString());
        ---
        ---
    }

protected void DeSerialize(string XmlString)
    {
        Category Cat = new Category();
        XmlDocument doc = new XmlDocument();
        doc.LoadXml (XmlString);
        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
        XmlSerializer ser = new XmlSerializer(Cat.GetType());
        object obj = ser.Deserialize(reader);
        // Then you just need to cast obj into whatever type it is, e.g.:
        Category myObj = (Category)obj;
        ---
        ---
    }

No comments:

Post a Comment