Jitesh Byahut 4/18/2014 4256

Serialization and Deserialization in C#

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or a persisted to a storage location. The storage location could be as database or file.

Deserialization is the process of using the serialized state information to recover the object from the serialized states to its original state.

When you transferring object, then you need to serialize the object and when you recover back that object, then you need to deserialize the object.

Types of Serialization

1. Binary Serialization

2. Soap Serialization

3. Xml Serialization

4. Json Serialization

Binary Serialization

Binary serialization convert object to the output stream that recover object back when needed.

For binary serialization demonstration, we are using a Class UserDetails that contains details of user. By using serialization concept we store these object details in to other location in form of a text file.

For a class to be serialized, you must add the [Serializable] attribute to the top of the class as follow:

[Serializable]

public class UserDetails

{

    public string Name { get; set; }

    public string Email { get; set; }

}

For Binary serialization use System.Runtime.Serialization.Formatters.Binary namespace.

The BinaryFormatter object is used to serialize and deserialize the object. Serialize() method is use to serialize the object and Deserialize() method is use to deserialize the object.

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;

 

    public void BinarySerialization()

    {

        // Store data into class object

        UserDetails user = new UserDetails();

        user.Name = "Jitesh";

        user.Email = "jitesh123.2008@gmail.com";

 

        FileStream file = new FileStream(@"D:\BinarySerialization.txt", FileMode.Create);

        BinaryFormatter formatter = new BinaryFormatter();

        formatter.Serialize(file, user);

        file.Close();

    }

Binary Deserialization

    public void BinaryDeserialization()

    {

        // Recover data from serialized state

        FileStream file = new FileStream(@"D:\BinarySerialization.txt", FileMode.Open);

        BinaryFormatter formatter = new BinaryFormatter();

        UserDetails user = (UserDetails)formatter.Deserialize(file);

        file.Close();

 

        // Show data on output

        Response.Write("UserName - "+user.Name);

        Response.Write("</br>");

        Response.Write("UserEmail - "+user.Email);

    }

    // Output

    // Jitesh

    // jitesh123.2008@gmail.com

If you want to ignore a member of class, then use [NonSerialized] attribute on top of that member.

Demo:

Soap Serialization

For soap serialization demonstration, we are using a Class UserDetails that contains details of user. By using serialization concept we store these object details in to other location in form of a text file.

[Serializable]

public class UserDetails

{

    public string Name { get; set; }

    public string Email { get; set; }

}

For soap serialization use System.Runtime.Serialization.Formatters.Soap namespace.

using System.Runtime.Serialization.Formatters.Soap;

using System.IO;

 

    public void SoapSerialization()

    {

        // Store data into class object

        UserDetails user = new UserDetails();

        user.Name = "Jitesh";

        user.Email = "jitesh123.2008@gmail.com";

 

        // Object convert into serialized state

        FileStream file = new FileStream(@"D:\Soap.txt", FileMode.Create);

        SoapFormatter formatter = new SoapFormatter();

        formatter.Serialize(file, user);

        file.Close();

    }

Soap Deserialization

    public void SoapDeserialization()

    {

        // Recover data from serialized state

        FileStream file = new FileStream(@"D:\Soap.txt", FileMode.Open);

        SoapFormatter formatter = new SoapFormatter();

        UserDetails user = (UserDetails)formatter.Deserialize(file);

        file.Close();

 

        // Show data on output

        Response.Write("UserName - " + user.Name);

        Response.Write("</br>");

        Response.Write("UserEmail - " + user.Email);

    }

    // Output

    // Jitesh

    // jitesh123.2008@gmail.com

Demo:

Xml Serialization

For xml serialization demonstration, we are using a Class UserDetails that contains details of user. By using serialization concept we store these object details in to other location in form of a text file.

In xml serialization, no need to use [Serializable] attribute on top of class as follow:

public class UserDetails

{

    public string Name { get; set; }

    public string Email { get; set; }

}

For xml serialization use System.Xml.Serialization namespace.

using System.Xml.Serialization;

using System.IO;

 

    public void XmlSerialization()

    {

        // Store data into class object

        UserDetails user = new UserDetails();

        user.Name = "Jitesh";

        user.Email = "jitesh123.2008@gmail.com";

 

        // Object convert into serialized state

        StreamWriter writer = new StreamWriter(@"D:\XmlSerialization.xml");

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

        serializer.Serialize(writer, user);

        writer.Close();

    }

Xml Deserialization

 

    public void XmlDeserialization()

    {

        // Recover data from serialized state

        FileStream file = new FileStream(@"D:\XmlSerialization.xml", FileMode.Open);

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

        UserDetails user = (UserDetails)serializer.Deserialize(file);

        file.Close();

 

        // Show data on output

        Response.Write("UserName - " + user.Name);

        Response.Write("</br>");

        Response.Write("UserEmail - " + user.Email);

    }

    // Output

    // Jitesh

    // jitesh123.2008@gmail.com

If you want to ignore a property, then use [Xmlignore] attribute on top of that property.

Demo:

Json Serialization

For json serialization demonstration, we are using a Class UserDetails that contains details of user. By using serialization concept we store these object details in to other location in form of a text file.

In Json serialization, need to add [DataContract] attribute on top of class and [DataMember] attribute on top of member, these attributes belongs to System.Runtime.Serialization namespace as follow:

[System.Runtime.Serialization.DataContract]

public class UserDetails

{

    [System.Runtime.Serialization.DataMember]

    public string Name { get; set; }

 

    [System.Runtime.Serialization.DataMember]

    public string Email { get; set; }

}

For json serialization use System.Runtime.Serialization.Json namespace.

using System.Runtime.Serialization.Json;

using System.IO;

 

    public void JsonSerialization()

    {

        // Store data into class object

        UserDetails user = new UserDetails();

        user.Name = "Jitesh";

        user.Email = "jitesh123.2008@gmail.com";

 

        // Object convert into serialized state

        FileStream file = new FileStream(@"D:\JsonSerialization.json", FileMode.Create);

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserDetails));

        serializer.WriteObject(file, user);

        file.Close();

    }

Json Deserialization

 

    public void JsonDeserialization()

    {

        // Recover data from serialized state

        FileStream file = new FileStream(@"D:\JsonSerialization.json", FileMode.Open);

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserDetails));

        UserDetails user = (UserDetails)serializer.ReadObject(file);

 

        // Show data on output

        Response.Write("UserName - " + user.Name);

        Response.Write("</br>");

        Response.Write("UserEmail - " + user.Email);

    }

    // Output

    // Jitesh

    // jitesh123.2008@gmail.com

If you want to ignore a property, then not need to use [DataMember] attribute on top of that member.

Demo:



Please give your feedback for improving this page