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








OOPS C#.Net ASP.NET ADO.NET
WCF WPF Web Services Silverlight
Java Script XML Html SQL Server Interview Questions
AJAX Control Toolkit AJAX VB.Net 64 HR Interview Questions
Design Pattern Entertainment LINQ .Net Interview Questions

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

Posted on
  • Thursday, August 11, 2011
  • by
  • Manish Agrahari
  • in
  • Labels: ,
  • 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.
    PROS
    • Follows a standard SOAP that other platforms can understand.
    • Serializes public and private members (deep serialization)
    CONS
    • 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
                                        //(ISerializable,SerializationInfo,StreamingContext)
    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);
                fileStreamObject.Close();
            }

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



            public static void Main()
            {
                Console.WriteLine("*********  SOAP Serialization *********");
                String file = @"C:\\SOAP_Serialization.xml";
                Employee objEmp = new Employee();
                objEmp.id = 1001;
                objEmp.name = "Manish Agrahari";
                objEmp.SetAddress("Noida");
                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: " + objEmployee.id);
                Console.WriteLine("Employee Name: " + objEmployee.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
                Console.ReadLine();


            }
        }
    }




     
         - <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/">
         - <SOAP-ENV:Body>
         - <a1:Employee id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SOAPSerializationByManishAgrahari/Polymorphism%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
          <id>1001id>
          <name id="ref-3">Manish Agrahariname>
          <address id="ref-4">Noidaaddress>
      a1:Employee>
      SOAP-ENV:Body>
      SOAP-ENV:Envelope>

    1 comments:

    Anonymous said...

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