Serialization Deserialization by Using BinaryFormatter

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called Deserialization.

This blog describes a code example of how to serialize and deserialize domain objects (class) using the 'BinaryFormatter' class.

Getting Started

BinaryFormatter Class comes under System.Runtime.Serialization.Formatters.Binary and serializes and deserializes an object, or an entire graph of connected objects, in binary format.

Code Example

The following code sample shows the implementation of "BinaryFormatter". The code defines a class named "MyBinaryFormatter" to demonstrate serialization and deserialization using the "BinaryFormatter" class.

It creates an object of a List collection named "Customers" to store the name of a Customer and adds four names to the collection. The code uses the "BinaryFormatter" class to write the state of the List object to a file named "Customer.dat" using the Serialize method of the BinaryFormattter class.

After serializing the list, the code restores the serialized data from the "Customer.dat" file, back into the form of the original object using the "Deserialize" method of the "BinaryFormatter" class, and creates a new list named "Customers2".

Finally displays the content of the newly created list, "Customer2", to check whether or not the data is deserialized in the correct format.

Code
 class MyBinaryFormatter  
   {  
     static void Main(string[] args)  
     {  
       List<string> Customers = new List<string>();  
       Customers.Add("Kailsh");  
       Customers.Add("Ramsharan");  
       Customers.Add("Panchanan");  
       Customers.Add("Roupya Manjari");  
       FileStream FS = new FileStream("Customer.txt", FileMode.Create);  
       Serialize(Customers, FS);  
       FS.Flush();  
       FS.Close();  
       FS = new FileStream("Customer.txt", FileMode.Open);  
       List<string> Customers2 = Deserialize(FS);  
       FS.Close();  
       if (Customers2 != null)  
       {  
         foreach (string Customer in Customers2)  
         {  
           Console.WriteLine(Customer);  
         }  
       }  
         Console.ReadLine();  
     }  
     private static void Serialize(List<string> customers, FileStream fs)  
     {  
       BinaryFormatter BF = new BinaryFormatter();  
       try  
       {  
         BF.Serialize(fs, customers);  
         Console.WriteLine("Successfully Serialized");  
       }  
       catch (Exception ex)  
       {  
         Console.WriteLine("Unable to Serialize from binary format");  
         //Console.WriteLine("Unable to deserialize from binary format");  
         Console.WriteLine(ex.Message);  
       }  
     }  
     private static List<string> Deserialize(FileStream fs)  
     {  
       BinaryFormatter BF = new BinaryFormatter();  
       List<string>LS=null;  
       try  
       {  
         LS = (List<string>)BF.Deserialize(fs);  
         Console.WriteLine("Successfully Deserialized");  
       }  
       catch (Exception ex)  
       {  
         Console.WriteLine("Unable to Deserialize from binary format");  
         //Console.WriteLine("Unable to deserialize from binary format");  
         Console.WriteLine(ex.Message);  
       }  
       return LS;  
     }  
   }  

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال