Saturday, October 4, 2008

Serialization in Asp.Net


Serialization, as implemented in the System.Runtime.Serialization namespace, is the process of serializing and deserializing objects so that they can be stored or transferred and then later recreated. Serializing is the process of converting an object into the linear seqence of bytes that can be stored or transferred.Deserializing is the process of converting a previously serialized sequence of bytes into an object.Basically if you want to store an object in a file for later retrieval, you store the output of serialization.The next time you want to read the objects, you call the deserialization methods, and your object is recreated exactly as it had been previosly.






How to serialize an Object:



At a high level, the steps for serializing an object are as follows:

1.Create a stream object to hold a serializing output.

2. Create a BinaryFormatter object(located in System.Runtime.Serialization.Formatters.Binary).

3. Call the BinaryFormatter.Serialize method to serialize the object, and output the result of the stream.



// C#

string data=”This must be stored in a file.”;



// Create file to save the data to

FileStream fs= new FileStream(”SerializedString.Data”,FileMode.Create);



// Create a BinaryFormatter object to perform the serialization

BinaryFormatter bf=new BinaryFormatter();



// Use the BinaryFormatter object to serialized the data to the file

bf.Serialize(fs, data);



// Close the file

fs.close();

1 comment:

Unknown said...

Hii kaniks, i view your posts its looking very informative,i m working as a S/w engineer in a reputated company, feel free to contact me i would like to help you
Cheers
Thanks with regards,
Nitin Saini