Thursday, August 11, 2011

Serialization in .Net – Part-4 (SOAP Serialization with Example)

SOAP Serialization:  

SOAP serialization is similar to XML serialization in that the objects being serialized are persisted as XML.
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML.

As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.

If portability is a requirement, use the SoapFormatter instead. Simply replace the formatter in the code above with SoapFormatter, and call Serialize and Deserialize as before. This formatter produces the following output for the example used above.
  • Serializes an object in SOAP format.
  • The type also needs to be marked with the [Serializable] atribute.
  • To use this class you need a reference to the System.Runtime.Serialization.Soap assembly.
  • This formatter also saves metadata (assembly and type information) on the output stream along with object data.
  • Follows a standard SOAP that other platforms can understand.
  • Serializes public and private members (deep serialization)
  • It is more verbose (less efficient) than BinaryFormatter.
  • It can NOT serialize generic collections (System.Collections.Generic namespace)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization; //<-- Namespace for Custom Serialization
using System.Runtime.Serialization.Formatters.Binary;//<-- Namespace for Binary Serialization
using System.Xml.Serialization;//<-- Namespace for XML Serialization
using System.Runtime.Serialization.Formatters.Soap;//<--Namespace for Soap Serialization

namespace SOAPSerializationByManishAgrahari
    [Serializable]//<-- This atribute is just required by BinaryFormatter and SoapFormatter
    public class Employee//<-- XMLSerializer needs that the class is defined as public 
        public int id;
        public string name;//<-- public properties to serialize. They  will be serialize by the three formatters. 
        private string address;//<--this private field won't be serialized using XmlSerialization.
        //They will be serialized using Binary or Soap formatters 
        [NonSerialized] //<-- Using this attribute, the field "gender"
                        //won't be serialized (by any of the three serializers) 
        //When using the BinaryFormatter or SoapFormatter classes to serialize an object,
        //use the [NonSerialized] Attribute attribute to prevent a field from being serialized.

        public string gender;
        //let's create a method to set the private properties. 
        public void SetAddress(string address)
            this.address = address;

        public string GetAddress()
            return this.address;

    class TestSerialization

        public void SOAPSerialize(string filename, Employee employeeObject)
            FileStream fileStreamObject = new FileStream(filename, FileMode.Create);
            SoapFormatter soapFormatter = new SoapFormatter();
            soapFormatter.Serialize(fileStreamObject, employeeObject);

        public object SOAPDeserialize(string filename)
            FileStream fileStreamObject = new FileStream(filename, FileMode.Open);
            SoapFormatter soapFormatter = new SoapFormatter();
            object obj = (object)soapFormatter.Deserialize(fileStreamObject);
            return obj;

        public static void Main()
            Console.WriteLine("*********  SOAP Serialization *********");
            String file = @"C:\\SOAP_Serialization.xml";
            Employee objEmp = new Employee();
   = 1001;
   = "Manish Agrahari";
            objEmp.gender = "Male";

            // Serializataion
            TestSerialization objTestSerialization = new TestSerialization();
            objTestSerialization.SOAPSerialize(file, objEmp);
            Console.WriteLine("Object Saved on Location: " + file);

            // Deserialization
            Employee objEmployee = (Employee)objTestSerialization.SOAPDeserialize(file);
            Console.WriteLine("*********  After Deserialization *********");
            Console.WriteLine("Employee ID: " +;
            Console.WriteLine("Employee Name: " +;
            Console.WriteLine("Employee Address: " + objEmployee.GetAddress());
            Console.WriteLine("Employee Gender: " + objEmployee.gender); //<-- Here Gender will be blank during deserialization
            //Because It could not be serialized due to [NonSerialized] Attrubute


     - <SOAP-ENV:Envelope xmlns:xsi=""    xmlns:xsd="" xmlns:SOAP-      ENC="" xmlns:SOAP-ENV="" xmlns:clr="" SOAP-ENV:encodingStyle="">
     - <SOAP-ENV:Body>
     - <a1:Employee id="ref-1" xmlns:a1="">
      <name id="ref-3">Manish Agrahariname>
      <address id="ref-4">Noidaaddress>

1 comment:

Anonymous said...

it was nice tutorial and keep updating us with new technology like MVC AND MVC RAZOR APPLICATION..