Serializing and De-Serializing the Objects.


//Required the following using directives
//using System;
//using System.IO;
//using System.Runtime.Serialization.Formatters.Binary;

//Serializing and De-Serializing the Objects:

private void SerializeObject(Object object, string fileName){

 Stream binaryStream = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite);
 try{
      BinaryFormatter binaryFormatter = new BinaryFormatter();
      binaryFormatter.Serialize(binaryStream, object);
 }finally{
      binaryStream.Close();
 }

}

private Object DeSerialiseObject(string fileName){

 BinaryFormatter binaryFormatter = new BinaryFormatter();
 Stream reader = File.Open(fileName, FileMode.Open, FileAccess.Read);
 try{
     Object object = binaryFormatter.Deserialize(reader);
 }finally{
      reader.Close();
 }               

 return object;
}


You can view this article online at:
http://www.kiranreddys.com/knowledgebase/index.php/article/serializing-and-de-serializing-the-objects