Tuesday, May 28, 2013

Using serialization and Deserialization in Asp.net | Example of serialization and Deserialization in asp.net

Serialization : Converting object to stream of bytes

DeSerialization : Doing reverse of serialization i.e Stream of bytes to object

Advantages of using serialization :

1. Transferring object from one domain to other.
2. passing object to web service.
3. Passing object from one application to other.

Example:

Now In the below example i have created a emp class and have mentioned [Serializable] above the method emp, so that after the object is created for this class it can be serialized. Now In the main method i will assign values to the emp object and then using binary formatter i will convert that object into binary format and then save it as a stream file. we can use any filestream name and any custom extensions. We can check the stream file created as it get saved in the following path :
application -- > bin--> Debug folder.

This stream file we can transfers wherever we require and then deserialize to convert back from binary format to object.

Code for Serialization :

using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication5
{
    class Class1
    {
        public static void Main(String[] args)
        {
            //----------Serialization------------------
            Emp Obj = new Emp();
            Obj.EmpId = 1;
            Obj.EmpName = "ChandanSingh";
            Obj.EmpSalary = "10000";


            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("SerializationFile.csl", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, Obj);
            stream.Close();
            Console.WriteLine("Serialization Process Completed");
            Console.ReadLine();
        }

    }

    [Serializable()]
    public class Emp 
    {
        public int EmpId;
        public string EmpName;
        public string EmpSalary;
    }

}


Code for DeSerialization :

using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication5
{
    class Class1
    {
        public static void Main(String[] args)
        {
        
            //-------- DeSerialization------------------
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("SerializationFile.csl", FileMode.Open, FileAccess.Read, FileShare.Read);
            Emp obj = (Emp)formatter.Deserialize(stream);
            stream.Close();

            Console.WriteLine("EmpId:" + obj.EmpId);
            Console.WriteLine("EmpName:" + obj.EmpName);
            Console.WriteLine("EmpSalary:" + obj.EmpSalary);
            Console.ReadLine();
        }

    }

    [Serializable()]
    public class Emp 
    {
        public int EmpId;
        public string EmpName;
        public string EmpSalary;
    }

}


Result:

Serialization 



DeSerialization :



No comments:

Post a Comment