SOAP Serialization :
SOAP Serialization and Binary Serialization having many similar properties.
In SOAP Serialization private fields, type information including assembly, namespace, public key token information are also serialized.
Deserialization gives the same object as it was before serialization.
Serialization can be controlled to exclude a field using [NonSerialized] attribute.
[Serializable] attribute is used to mark a class as serializable.
Classes do not need to have a parameterless constructor. Because of performance issues, the constructor of the class is not called during deserialization.
Use if the serialization and deserialization platforms are not .NET.
Use if the message is sent through a firewall.
Here ClsSample is a Serializable class. It has private and Public members,
using System;
using System.Collections.Generic;
using System.Text;
namespace SOAPSerilization
{
[Serializable]
public class ClsSample
{
private int m_TotalCount = 5;
public String m_StrWord = "Ten";
public void ReAssignMemberValues()
{
m_TotalCount = 20;
m_StrWord = "Twenty";
}
public void DisplayMemberValueAfterDeSerialization()
{
Console.WriteLine("Private member value is " + m_TotalCount);
Console.WriteLine("Public member value is " + m_StrWord);
}
}
}
Following way you can Serialize and Deserialize the objects(through files)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
namespace SOAPSerialization
{
public class ClsSOAP
{
//Serialization
public void ToSoapSerialization(Object objToSoap,string filePath)
{
IFormatter formatter;
FileStream fileStream = null;
try
{
fileStream = new FileStream(filePath,FileMode.Create, FileAccess.Write);
formatter = new SoapFormatter();
formatter.Serialize(fileStream, objToSoap);
}
catch (Exception exception)
{
throw exception;
}
finally
{
if (fileStream != null) fileStream.Close();
}
}
//Deserialization
public object FromSoapSerialization(string filePath)
{
IFormatter formatter;
FileStream fileStream = null;
try
{
fileStream = new FileStream(filePath,FileMode.Open, FileAccess.Read);
formatter = new SoapFormatter();
return formatter.Deserialize(fileStream);
}
catch (Exception exception)
{
throw exception;
}
finally
{
if (fileStream != null) fileStream.Close();
}
}
}
}
//Main Class
using System;
using System.Collections.Generic;
using System.Text;
namespace SOAPSerialization
{
public class Program
{
static void Main(string[] args)
{
ClsSample ObjSample;
ClsSOAP ObjSOAP;
ObjSample = new ClsSample();
ObjSample.ReAssignMemberValues();
ObjSOAP = new ClsSOAP();
//Serialize
ObjSOAP.ToSoapSerialization(ObjSample, "c:\\SOAPSerialized.TXT");
ObjSample = null;
//Deserilize
ClsSample ObjDeseialSOAP;
ObjDeseialSOAP = (ClsSample)ObjSOAP.FromSoapSerialization("c:\\SOAPSerialized.TXT");
ObjDeseialSOAP.DisplayMemberValueAfterDeSerialization();
}
}
}
Output:
Private member value is 20
Public member value is Twenty
Here both private and public members are serialized
No comments:
Post a Comment