Serialize and Deserialize Domain Object Using SoapFormatter in C#

Kailash Chandra Behera | Saturday, December 30, 2017

Introduction

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 how to serialize and deserialize domain object(class) using 'SoapFormatter' class.

Getting Started

SoapFormatter Class comes under System.Runtime.Serialization.Formatters.Soap and serializes and deserializes an object, or an entire graph of connected objects, in SOAP format.

Code Example

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

It creates an object of List collection named "Customers" to store the name of the "Customer" and also adds four names to the collection. The code uses the "SoapFormatter" 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 resisters the serialized data from the "Customer.dat" file, back to the form of an object using the "Deserialize" method of the "SoapFormatter" class, and creates a new list nemed "Customers2".

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

Code

 classMySoapFormatter  
   {  
     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.soap",FileMode.Create);  
       Serialize(Customers, FS);  
       FS.Flush();  
       FS.Close();  
       FS =new FileStream("Customer.soap",FileMode.Open);  
       List<string> Customers2 = Deserialize(FS);  
       FS.Close();  
       if (Customers2 != null)  
       {  
         foreach (string Customer in Customers2)  
           Console.WriteLine(Customer);  
         }  
       }  
       Console.ReadLine();  
     }  
     public static void Serialize(List<string> customers, FileStream fs)  
     {  
       SoapFormatter SF =new SoapFormatter();  
       try  
       {  
         SF.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);  
       }  
     }  
     public static List<string> Deserialize(FileStream fs)  
     {  
       SoapFormatter SF =new SoapFormatter();  
       List<string> LS = null;  
       try  
       {  
         LS = (List<string>)SF.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