Reader Level:
ARTICLE

Generic Way of Serializing and DeSerializing the Object as Binary Data Using Binary Formatter ASP.NET C#

Posted by Lajapathy Arun Articles | ASP.NET Programming April 25, 2012
In this article we are going to see, how we serialize and deserialize an object as binary data in a generic way using the binary formatter.
  • 0
  • 0
  • 6089

In this article we are going to see, how we serialize and deserialize an object as binary data in a generic way using the binary formatter.

Step 1: Used Namespace:

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

Step 2: Usage:

protected void Page_Load(object sender, EventArgs e)
{
    Employees emps = new Employees();
    emps.Add(new Employee("1", "Lajapathy"));
    emps.Add(new Employee("2", "Anand"));
    emps.Add(new Employee("3", "Sathiya"));
    emps.Add(new Employee("4", "Lakshmi"));
    emps.Add(new Employee("5", "Parthiban"));
    string pth = @"D:\Test.bin"

    //Serializing the collection
    Serialize<Employees>(emps, pth);
    //Deserializing the collection
    DeSerialize(pth);
} 

Step 3: Serializing the Object using Binary Formatter:

//Serializing the List
public void Serialize<T>(T emps, String filename)
{
    //Create the stream to add object into it.
    System.IO.Stream ms = File.OpenWrite(filename); 
    //Format the object as Binary
    BinaryFormatter formatter = new BinaryFormatter();
    //It serialize the employee object
    formatter.Serialize(ms, emps);
    ms.Flush();
    ms.Close();
    ms.Dispose();
}

Step 4: DeSerializing the Object using Binary Formatter:

//Deserializing the List

public T Deserialize<T>(String filename)
{
    //Format the object as Binary
    BinaryFormatter formatter = new BinaryFormatter();
    //Reading the file from the server
    FileStream fs = File.Open(filename, FileMode.Open);
    //It deserializes the file as object.
    object obj = formatter.Deserialize(fs);
    //Generic way of setting typecasting object.
    T emps = (T)obj;
    fs.Flush();
    fs.Close();
    fs.Dispose();
    return emps;
}

Step 5: Deserializing the List and displaying it:

public void DeSerialize(String filename)
{
    Employees emps = Deserialize<Employees>(filename);
    foreach (Employee employee in emps)
    {
        Response.Write(employee.Name + "<br/>");
    }
}

Step 6: Output Path of Serialization:

image1.jpg

Step 7: Output of DeSerialization:

image2.jpg
Step 8: Copy & paste code snippet:

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

namespace SampleApplication
{
    public partial class GenericObjectSerialization : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Employees emps = new Employees();
            emps.Add(new Employee("1", "Lajapathy"));
            emps.Add(new Employee("2", "Anand"));
            emps.Add(new Employee("3", "Sathiya"));
            emps.Add(new Employee("4", "Lakshmi"));
            emps.Add(new Employee("5", "Parthiban"));
 
            string pth = @"D:\Test.bin";
            //Serializing the collection
            Serialize<Employees>(emps, pth);
            //Deserializing the collection
            DeSerialize(pth);
        }
        //Serializing the List
        public void Serialize<T>(T emps, String filename)
        {
            //Create the stream to add object into it.
            System.IO.Stream ms = File.OpenWrite(filename);
            //Format the object as Binary
           
BinaryFormatter formatter = new BinaryFormatter();
            //It serialize the employee object
            formatter.Serialize(ms, emps);
            ms.Flush();
            ms.Close();
            ms.Dispose();
        }
        //Deserializing the List
        public T Deserialize<T>(String filename)
        {
            //Format the object as Binary
            BinaryFormatter formatter = new BinaryFormatter();
             //Reading the file from the server
            FileStream fs = File.Open(filename, FileMode.Open); 
            //It deserializes the file as object.
            object obj = formatter.Deserialize(fs);
            //Generic way of setting typecasting object.
            T emps = (T)obj;
            fs.Flush();
            fs.Close();
            fs.Dispose();
            return emps;
        }
        //Deserializing the List and displaying it.
        public void DeSerialize(String filename)
        {
            Employees emps = Deserialize<Employees>(filename);
            foreach (Employee employee in emps)
            {
                Response.Write(employee.Name + "<br/>");
            }
        }
    }
    //Classes
    [Serializable]
    public class Employee
    {
        public Employee(String id, String name)
        {
            _ID = id;
            _Name = name;
        }
        private String _ID = String.Empty;
        private String _Name = String.Empty;
        public String ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
    }
    [Serializable]
    public class Employees : CollectionBase
    {
        //Constructor
        public Employees()
        {
 
        }
        //Add function
        public void Add(Employee objT)
        {
            this.List.Add(objT);
        }
        //Indexer
        public Employee this[int i]
        {
            get
            {
                return (Employee)this.List[i];
            }
            set
            {
                this.List.Add(value);
            }
        }
    }

Thanks for reading this article. Have a nice day. 

COMMENT USING

Trending up