Serialization is the process of converting an object into a form so that it can be persisted int the storage location(memory,a database or a file) or can be transported across the network.Main purpose of Serialization 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 object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
Serailization usage:
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange.By using Serailizaton,we can send the object to a remote application by means of web service,passing an object from one domain to another be means of .net remoting,passing an object through firewall as an XML string or passing the user specific information across applications.
How do you use serailizataion:
Serialization is handled by classes and interfaces in the System.Runtime.Serialization namespace.you need to do two things to serialize an object.
1)a stream to contain the serialized objects
2)a formatter to serialize the objectds into the stream.
.NET serialization formats:
There are three fromats of serialization.
1) Binary Serialization : Light and compact used in Remoting
2) SOAP Serialization : Interoperable use SOAP and used in web Services
3) XML Serialization : Custom Serialization
SOAP and Binary Serialization:
Serialization is always needed when you transfer an object across a network.Both XML and Binary Serialization can serialize the entire object and all those object that are being refrenced by it.This is why it is also called Deep Serialization.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.If you want any class to serialize through any of these methods you should mark the class as [Serializable].
XML Serilization:
XML Serilization serialize only the public members.For XML Serialization,you need to use [Serializable] attribute and specify them for every public member which you to serialize.This is why it is also called Shallow Serialization.
Note : The [NonSerialized] Attribute can be used to keep specific fields from being serialized.
Serialize an object with Binary Format (Binary Serialization Example) :
I am trying to give an example of Binary Serialization,which converts an employee object into stream(i saved it in a text file) and recreating the object by using stream which is saved in text file.
Example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Serialization
{
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee();
string filename = @"C:\Serialization.txt";
WriteToFile(emp,filename);
ObjectFromFile(filename);
Console.ReadLine();
}
public static void WriteToFile(Employee emp, String filename)
{
Stream stream = File.OpenWrite(filename);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, emp);
stream.Close();
}
public static void ObjectFromFile(String filename)
{
Stream stream = File.OpenRead(filename);
BinaryFormatter formatter = new BinaryFormatter();
stream.Position = 0;
object o = formatter.Deserialize(stream);
stream.Close();
Employee emp = (Employee)o;
Console.WriteLine(emp.Empid + " " + emp.EmpName);
}
}
[Serializable]
class Employee
{
private int empid;
private string empName;
public int Empid
{
get
{
return empid;
}
set
{
this.empid = value;
}
}
public string EmpName
{
get
{
return empName;
}
set
{
this.empName = value;
}
}
public Employee()
{
empid = 1;
empName = "Johny";
}
}
}
output in console:
1 Johny
I hope you can better understand what is a serialization in .NET and How to serialize an object and reconstruct the object by using Binary,SOAP serialization.