06 May 2015

Serialization vs DeSerialization

Serialization is the process of converting an object into some data format such as XML or stream of bytes in order to store the object to a memory, or 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.


You can serialize an object and transport it over the internet using HTTP between a client and a server. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format for storage or transport.


Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.


System.XML.Serialization/System.Runtime.Serialization contains the classes necessary for serializing and deserializing objects into XML format and Binary format respectively


Binary format serialization is faster than XML serialization, because Binary format writes the raw data but XML format serialization needs formatting of data as per XML


In Binary format serialization all members will be serialized.




  1. XML serialization only serializes pubic fields and properties

  2. XML serialization does not include any type information

  3. We need to have a default/non-parameterized constructor in order to serialize an object

  4. ReadOnly properties are not serialized


Let's start with a basic example. Here is a simple class the need to be serialized :



public class AddressDetails
{
public int HouseNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
private string PoAddress { get; set; }
}

Code to serialize the above class:
using System.Xml.Serialization;
using System.IO;

public static void Main(string[] args)
{
AddressDetails details = new AddressDetails();
details.HouseNo = 4;
details.StreetName = "Rohini";
details.City = "Delhi";
Serialize(details);
}
static public void Serialize(AddressDetails details)
{
XmlSerializer serializer = new XmlSerializer(typeof(AddressDetails));
using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
{
serializer.Serialize(writer, details);
}
}

The output after the serialization is :

<?xml version="1.0" encoding="utf-8"?>

<AddressDetails>
<HouseNo>4</HouseNo>
<StreetName>Rohini</StreetName>
<City>Delhi</City>
</AddressDetails>

Serialization can be of the following types:

  1. Binary Serialization

  2. SOAP Serialization

  3. XML Serialization


If the server and client application are .NET applications, the user can make use of binary serialization. If the client and server use two different types of systems, then the user can make use of XML type serialization. XML serialization is useful when user wants full control of how the property can be serialized. It supports XSD standard.


Remoting and Web Services depend heavily on Serialization and De-serialization.


XML Serialization Attributes


XmlAttribute: This member will be serialized as an XML attribute
XmlElement: The field will be serialized as an XML element, ex: [XmlElement("Number")]
XmlIgnore: Field will be ignored while Serialization
XmlRoot: Represent XML document's root Element

No comments: