.Net Serialization using Soap Formatter

Serialization is a method to persist the state of an object in order to have the ability to recreate the same object when required. when the object is required to travel electronically over the network, in such cases the objects are serialized at one end and deserialized at the other end. Serialization is one of the fundamental requirements for techniques such as .NET Remoting.
 
Serialization in .NET are of four types as shown in:
 
  1. XML serialization allows you to serialize objects into an XML data format. However, it can convert only the public properties of an object into XML data. Therefore it is referred to as shallow serialization.
  2. In SOAP and BINARY serialization technique the state of the entire object is serialized into a stream of bytes. In cases where the object contains a reference to other objects, even those are serialized. This type of serialization is known as deep serialization.This overcomes the disadvantage of XML serializer to serialize the private fields of an object.
The .NET Framework also provides the SoapFormatter and the BinaryFormatter classes. These classes implement the IRemotingFormatter and the IFormatter interfaces.
The main methods which encapsulate the functionality of serialization are the Serialize and Deserialize methods.
 
Here we study the soap formatter"..
  1. [Serializable]  
  2. public class secreteClass  
  3. {  
  4.     private int myaccountnumber = 0;  
  5.     [NonSerialized()]  
  6.     private string password = "qwerty321";  
  7.   
  8.     public int MyAccNum  
  9.     {  
  10.         get  
  11.         {  
  12.             return myaccountnumber;  
  13.         }  
  14.         set  
  15.         {  
  16.             myaccountnumber = value;  
  17.         }  
  18.     }  
  19.     public string Passwd  
  20.     {  
  21.         get  
  22.         {  
  23.             return password;  
  24.         }  
  25.         set  
  26.         {  
  27.             password = value;  
  28.         }  
  29.     }  
  30.     public secreteClass()  
  31.     {  
  32.   
  33.     }  

Note: Here password is not serialized and hence it is shown as
  1. [NonSerialized()]  
  2. using System;  
  3. using System.IO;  
  4. using System.Collections;  
  5. using System.Runtime.Serialization;  
  6. using System.Runtime.Serialization.Formatters.Soap;  
  7. public static void Main()  
  8. {  
  9.     try  
  10.     {  
  11.         secreteClass objSecrete = new secreteClass();  
  12.         objSecrete.MyAccNum = 123321456;  
  13.   
  14.         //Soap serialization technique  
  15.         SoapFormatter formatter = new SoapFormatter();  
  16.         Stream objfilestream = new FileStream("c:\\Myserialzed.xml", FileMode.Create, FileAccess.Write, FileShare.None);  
  17.   
  18.         formatter.Serialize(objfilestream, objSecrete);  
  19.         objfilestream.Close();  
  20.         //deserialization  
  21.         Stream objreadstream = new FileStream("c:\\Myserialzed.xml", FileMode.Open, FileAccess.Read, FileShare.Read);  
  22.         secreteClass objSecrete2 = (secreteClass) formatter.Deserialize(objreadstream);  
  23.   
  24.         int Myaccno = objSecrete2.MyAccNum;  
  25.         Console.Writeln("AccNo:{0} " + Myaccno.ToString());  
  26.     }  
  27.     catch (Exception ex)  
  28.     {}  

Note: When building this code, you must reference the System.Runtime.Serialization.Formatters.Soap.dll assembly.
 
This is the XML file(Myserialzed.xml) screenshot, where the data is serialized and stored.
 
Myserialized.xml
  1. <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">  
  2.     <SOAP-ENV:Body>  
  3.         <a1:serderlize_x002B_secreteClass id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/assem/App_Web_1imlsxqk%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">  
  4.             <myaccountnumber>123321456</myaccountnumber>  
  5.         </a1:serderlize_x002B_secreteClass>  
  6.     </SOAP-ENV:Body>  
  7. </SOAP-ENV:Envelope> 
In the next article will see other types of serialization techniques.