Introduction
Serialization is the process of converting an object or a graph of objects into a linear sequence of bytes for either storage or transmission to another location. Deserialization is the process of taking in stored information and recreating objects from it.
The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects. Classes in the System.Runtime.Serialization.Formatters namespace control the actual formatting of various data types encapsulated in the serialized objects.
The following class defines an object to be serialized. Note that the class is prefixed with "SerializationAttribute".It indicates that a class can be serialized. It is a specialized attribute that derives from System.Attribute.
[SerializableAttribute] public class PersonalData { int id; string myname; public PersonalData() { this.id=1200; this.myname="balamurali"; } public void Show() { Console.WriteLine (id); Console.WriteLine (myname); } }
An instance of System.Runtime.Serialization.Formatters.Soap.SoapFormatter class is used to Serialize and deserialize an object, or an entire graph of connected objects, in SOAP format.
public class SerialDataSoap { public static void Main() { // Serialization PersonalData pdata = new PersonalData(); SoapFormatter sf = new SoapFormatter(); FileStream fs = new FileStream(@"c:\data.txt",FileMode.Create ); sf.Serialize(fs,pdata); fs.Close(); Console.WriteLine ("SOAP Serialization successful"); // De-Serialiation FileStream fs1 = new FileStream(@"c:\data.txt",FileMode.Open); PersonalData pdata1 = (PersonalData) sf.Deserialize(fs1); pdata1.Show(); Console.WriteLine ("SOAP De-Serialization successful"); } }
On Serialization, you get a file "data.txt"which is in soap format. On Deserialization, object is constructed using this file and we can access data members and methods using the newly constructed object. Following is the content of "data.txt".
{h2}On Serialization, you get a file "data.txt"which is in soap format. On Deserialization, object is constructed using this file and we can access data members and methods using the newly constructed object. Following is the content of "data.txt".{/h2}
{SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"} {SOAP-ENV:Body} {a1:Data id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleTest/ConsoleTest%2C%20Version%3D1.0.2219.25747%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"} {id}1200{/id} {myname id="ref-3"}balamurali{/myname} {/a1:Data} {/SOAP-ENV:Body} {/SOAP-ENV:Envelope}
An instance of System.Runtime.Serialization.Formatters.Binary.BinaryFormatter class is used to Serialize and deserialize an object, or an entire graph of connected objects, in Binary format.
public class SerialDataBinary { public static void Main() { // Serialization PersonalData pdata = new PersonalData(); BinaryFormatter bform = new BinaryFormatter(); FileStream fs = new FileStream(@"c:\sample.txt",FileMode.Create ); bform.Serialize(fs,pdata); fs.Close(); Console.WriteLine ("Binary Serialization successful"); // De-Serialiation FileStream fs1 = new FileStream(@"c:\sample.txt",FileMode.Open); PersonalData pdata1 = (PersonalData) bform.Deserialize(fs1); pdata1.Show(); Console.WriteLine ("Binary De-Serialization successful"); } }
On Serialization, you get a file "sample.txt"which is in binary format. On Deserialization, object is constructed using this file and we can access data members and methods using the newly constructed object.
|
| Author: Rajendra J Rana 07 Feb 2006 | Member Level: Bronze Points : 0 |
The article given here has given very nice explantion about serialization and deserialization concept . I got good explanation from this article thanks to Author.
|