Friday, June 21, 2013

Serialization and Deserialization in C#

Serialization is a process of converting object into stream of bytes. And deserialization is a process of getting object back from serialization form.

According to MSDN "Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization."

The bellow figure shows how serialization works.


















Deserialization is just opposite of serialization. It is the mechanism to getting object back from stream of bytes. 



















There are three types of serialization mechanism available in C#.


  • XML Serialization
  • Binary Serialization
  • Soap Serialization


XML Serialization

XML Serialization serialize only public fields, properties of an object and parameters and return value of a method. That is why it is called Shallow Serialization.  XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization has all the classes and method need for XML serialization. more

Binary Serialization

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams. It is also known as deep serialization. Because it serialize every thing of an object. more

SOAP Serialization

SOAP serialization serialize an object into XML stream with SOAP standard. This serialization is used to transmit data over network. Most common area where SOAP serialization is used is web services. more



Download

You can download examples and source code from here.