Thursday, 22 March 2012

What is Serialization and Why Serialization Process?

Serialization in .net?

Serialization is a process of converting the object form to an array of bytes of data. Using streams which can be moved form one place to another palce. As well as from physical form data to again form a object is called Deserialization.

Why Serialization process?

Serialization is used to save  the state of object. And transfer the data across the network required the object serialization.
The ISerializable interface allows you to make any class Serializable. Which is System.xml.Serialization namespace.

In .net we have 3 approaches to make serialization
1.Binary serialization 2. Soap serialization 3. Xmlserialization(custom serialization,shallow serialization)

 Xml Serialization

To make object save as a xml file we have to choose xml serialization process. System.xml and System.Xml.Serialization are namespaces.
Here we have the relavent classes to make serialization.

For Serialization and Deserialization for custom object
[Serializable()]
public class ObjectToSerialize : ISerializable  // Employee
{
// For Deserialization
ObjectToSerialize(SerializationInfo info, StreamingContext context, bool ConstructSchema);
// Serialization
public virtual void GetObjectData(SerializationInfo info, StreamingContext context);
}

Serialize() and deserialize() method belong to that particular type formatters. Like
Xmlserilizer------------------serialize and deserilizer methods
Binaryserlizer-----------------serializer and derrilser methods
Soap serializer-----------------serializer and deserlizer methods
But  these are derived from IFormatter for standardization.

Coming to xmlserialization
Type(DataSet….Employee…ect )should be Public  and members also should be public ----xmlserialization
Custom Object Serialization
[Serializable]
     public class Employee:ISerializable
    {

        private int EmpId;

        public int EmpId1
        {
            get { return EmpId; }
            set { EmpId = value; }
        }
        public string EmpName;

        //Default constructor
        public Employee()
        {
            EmpId = 0;
            EmpName = null;
        }

 public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("EmpId10", this.EmpId);          
        }
    }
SerializationInfo info: This object holds a name-value pair for the properties to be serialized. You can decide which property   should be serialized and which not in the GetObjectData() function. All the properties that are added to this SerializationInfo parameter will be serialized. Here are the codes for the two functions. Add them to our Employee class.
static void Main()
        {
          Employee eobj = new Employee();
            eobj.EmpId1 = 1041;
          }
Xml Serialization


Serialization

String strxml=@"C:\EmployeeInfo.xml");

Type z = eobj.GetType();

XmlSerializer serializer = new XmlSerializer(z);
            Or
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
TextWriter textWriter = new StreamWriter(strxml); // return type StramWriter
serializer.Serialize(textWriter, eobj);
textWriter.Close();------------serialization
DeSerialization

XmlSerializer serializer = new XmlSerializer(typeof(Employee));

TextReader textReader = new StreamReader(strxml);
object  eobs=serializer.Deserialize(textReader);
textReader.Close();

Employee eobjjj = new Employee();
eobjjj = (Employee)eobs;
int s = eobjjj.EmpId1;
Console.Write(s);
Whatever object properties we want to serialize those should be inside the
GetDataObject funcation why because Serialization of object starts from GetDataObject.

No comments:

Post a Comment