Serialization in .Net – I
Serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link, either in binary form, or in some human-readable text format such as XML. The series of bytes or the format can be used to re-create an object that is identical in its internal state to the original object (actually a clone).
A class can serialize / Deserialize using [Serializable] attribute to the class.
[Serializable]
class MyClass
{
//Class implementation..
}
To serialize an object, in .Net framework the BinaryFormatter class can be used.
System.IO.Stream sw = System.IO.File.OpenWrite("C:\\Sample.bin");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _Formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Class1 objClass = new Class1();
objClass.Username = "Username1";
_Formatter.Serialize(sw,objClass);
sw.Flush();
sw.Close();
This will serialize the objClass in to the C:\Sample.bin file.