What is Serialization in .NET, types of Serialization and why we need it while developing an application?
By in .NET on Jul 27 2006
  • Kavitha Jagadal
    Jan, 2013 23

    Serialization: 1.Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache. 2.Serialization is the technology that enables an object to be converted into a linear stream of data that can be easily passed across process boundaries and machines. This stream of data needs to be in a format that can be understood by both ends of a communication channel so that the object can be serialized and reconstructed easily. Advantage: Using serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format. Serialization is used by Remoting, Web Services SOAP for transmitting data between a server and a client. The Remoting technology of .NET makes use of serialization to pass objects by value from one application domain to another. De-serialization is the reverse; it is the process of reconstructing the same object later. Types of Serialization Serialization can be of the following types: 1.Binary Serialization 2.SOAP Serialization 3.XML Serialization Binary Serialization: Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically. The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media. Difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not. In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved. Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object SOAP Serialization: The SOAP protocol is ideal for communicating between applications that use heterogeneous architectures. In order to use SOAP serialization in .NET we have to add a reference to System.Runtime.Serialization.Formatters.Soap in the application. The basic advantage of SOAP serialization is portability. The SoapFormatter serializes objects into SOAP messages or parses SOAP messages and extracts serialized objects from the message. XML Serialization: · According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or the parameters and returns values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. · XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform." Implementing XML Serialization in .Net is quite simple.

    • 5
  • Gaurav Agrawal
    May, 2012 17

    The serialization is the process of converting the objects into stream of bytes. they or used for transport the objects(via remoting) and persist objects(via files and databases) When developing smaller applications that do not have a database (or other formal storage mechanism) or data that doesn't need to be stored in a database (such as the state of a web application), you often still would like to save the data for later retrieval. There are many ways to do this, but many of them are subject to a lot of extra code (work) and extra time spent debugging. With .NET, there is now an easy way to add this functionality to your code with only a few lines of easily tested code. This easy way is called serialization. Serialization is the process of storing an object, including all of its public and private fields, to a stream. Deserialization is the opposite – restoring an object's field values from a stream. The stream is generally in the form of a FileStream, but does not have to be. It could be a memory stream or any other object that is of type IO.Stream. The format can be anything from XML to binary to SOAP.Reference : http://planetofcoders.com/serialization-net/

    • 1
  • arif desai
    Feb, 2012 10

    Serialization is a process of taking an object and converting into a form so that it can be transported across the network or can be persisted in the storage location. This storage location can be physical file, database or ASP.NET Cache. The form contains the state of the object so that by this format, we can construct the same object a later point in time, which is called Deserialization.

    There are three formats of serialization

    Binary Serialization: Light and compact used in Remoting

    SOAP Serialization  : Interoperable use SOAP and  used in web Services

    XML Serialization : Custom Serialization

    XML Serialization

    For XML serialization, you need to use the attributes and specify them for each and every public member that you need. But since it is limited that it can serialize only public members, Serization done by it is called custom serialization. It is also known as Shallow Serialization

    SOAP and Binary Serialization

    XML serializes only public members of the class. You use SOAP or Binary serialization when you need to transport data across the network. SOAP sends it using HTTP Protocol which makes it most interoperable while Binary serialization is known for its light and compact nature. Web Services uses the SOAP Serialization and Remoting uses the Binary Serialization. Infact Serialization is always neccessary when you need the object to transfer across a network. Advantage of using the SOAP or Binary serialization is that you can serialize the entire object and all those object that are being refrenced by it. This is why it is also called Deep Serialization.  If you want any class to serialize through any of these methods then you should use [Serializable] attribute on that class and then you can use the SoapFormater class or BinaryFormatter class to do the serialization. These classes have Serialize and DeSerialize method.  If you will not use SerializableAttribute for the class, then it will raise the exception.

    Though this is the easiest way but at time you need the way so that you can decide what fields to serialize and how the serialization actually occurs. You can implement the ISerializable interface in the class. You need two things for that

    1. Constructor that is overridden and can handle the Deserialization process
    2. GetObject method that tracks about which data is serialized.

    A small example below illustrate this all.

    public class Employee :ISerializable

    {

    private int emp_no;

    private string name;

    protected TestData(SerializationInfo info,StreamingContext)

    {
    this.emp_no = info.GetInt32("emp_no");
    this.name = info.GetString("name");
    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)

    {
    info.AddValue("emp_no", this.emp_no);
        info.AddValue("name", this.name);
    }


    I hope this introductory serialization information can help you to understands about Serialization with its need  and its effective usage.

    • 1
  • Abhishek Navendu
    May, 2011 24

    Serialization is the process of converting complex objects into stream of bytes for storage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form. The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serialization namespace. The ISerializable interface allows you to make any class Serializable.

    Here are the following steps that we are going to do to create a serializable class and test it.

    • Create a custom class named Employee and assign properties.
    • Define the serialization functions.
    • Create a main class and instantiate our Employee class.
    • Serialize the object to a sample file.
    • Deserialize the values by reading it from the file.

    Defining Employee class and properties
    Our custom class Employee should be derived from the ISerializable interface and should hold the Serializable attribute. Here is the code snippet.

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary; namespace MyObjSerial
    {
    [Serializable()] //Set this attribute to all the classes that want to serialize
    public class Employee : ISerializable //derive your class from ISerializable
    {
    public int EmpId;
    public string EmpName;

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

    Different types of serialization.

    The different types of serialization are

    ·         Binary Serialization

    ·         XML Serialization

    ·         SOAP Serialization

    Binary Serialization

    Binary serialization is the process where you convert your .NET objects into byte stream. In binary serialization all the public, private, even those which are read only, members are serialized and converted into bytes. So when you want a complete conversion of your objects to bytes then one can make use of binary serialization.

    XML Serialization

    In XML serialization only the public properties and fields of the objects are converted into XML. The private members are not taken into consideration in XML serialization.

    SOAP Serialization

    Similar to XML serialization. When you serialize object to SOAP format it conforms to the SOAP specification.

    Need of Serialization

    Most uses of serialization fall into two categories: persistence and data interchange. Persistence allows us to store the information on some non-volatile mechanism for future use. This includes multiple uses of our application, archiving, and so on. Data interchange is a bit more versatile in its uses. If our application takes the form of an N-tier solution, it will need to transfer information from client to server, likely using a network protocol such as TCP. To achieve this we would serialize the data structure into a series of bytes that we can transfer over the network. Another use of serialization for data interchange is the use of XML serialization to allow our application to share data with another application altogether. As you can see, serialization is a part of many different solutions within our application.

    • 1
  • haribabu
    Jan, 2009 12

    hi manoj

     this is the nice article.can you please provide me the next article which u written on custom serialization.

    thanks
    harry

    • 1
  • Munesh Sharma
    May, 2016 23

    Binary serialization (Save your object data into binary format) Soap Serialization (Save your object data into binary format; mainly used in network related communication). XmlSerialization (Save your object data into an XML file).

    • 0
  • rahul kumar
    Apr, 2016 26

    Serialization can be defined as the process of converting the state of an object instance to a stream of data, so that it can be transported across the network or can be persisted in the storage location. The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent state of an object to a storage medium so an exact copy can be recreated at a later stage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form.There are three types of serialization in .Net : Binary Serialization, SOAP Serialization and XML Serialization. More in detail....http://net-informations.com/faq/net/serialization.htm

    • 0
  • pawan kumar
    Feb, 2016 6

    Asp.Net Serialization & Deserialization with C#.NetI found a good demo on http://sourcecodehub.com/article/404/aspnet-serialization-deserialization-with-cnetAlso I have downloaded the source codes from there

    • 0
  • Prakash K
    May, 2015 23

    Serialization : When developing smaller applications that do not have a database (or other formal storage mechanism) or data that doesn't need to be stored in a database (such as the state of a web application), you often still would like to save the data for later retrieval. There are many ways to do this, but many of them are subject to a lot of extra code (work) and extra time spent debugging. With .NET, there is now an easy way to add this functionality to your code with only a few lines of easily tested code. This easy way is called serialization.

    • 0
  • Sekhar Chandra
    Oct, 2010 22

    Very good article

    • 0
  • ramsharan choudhary
    Aug, 2010 30

    Great Work.
    Nice article

    • 0
  • Pritom Nandy
    Apr, 2010 21

    A very nice post. Thank you...

    • 0
  • Sarah Nasir
    Mar, 2010 3

    Manoj. your explanation is very nice. but the links are broken.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS