Wednesday, July 1, 2009

Types of Serialization

serialization is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link.

Emp emp = new Emp();
emp.Id = 1;
emp.Name = "S.Bala";
MemoryStream ms = new MemoryStream();

//Binary
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(ms, emp);
ms.Position = 0;
Emp empGet = binaryFormatter.Deserialize(ms) as Emp;
ms.Close();

//SOAP
ms = new MemoryStream();
System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
soapFormatter.Serialize(ms, emp);
ms.Position = 0;
empGet = new Emp();
empGet = soapFormatter.Deserialize(ms) as Emp;
ms.Close();

//XML
ms = new MemoryStream();
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Emp));
xs.Serialize(ms, emp);
ms.Position = 0;
empGet = new Emp();
empGet = xs.Deserialize(ms) as Emp;
ms.Close();


[Serializable]
public class Emp
{
public int Id;
public string Name;
}

No comments: