The SOAP protocol is ideal for communicating between
applications that use heterogeneous architectures. In order to use SOAP
serialization in .NET we have to add a reference to
System.Runtime.Serialization.Formatters.Soap in the application. The basic
advantage of SOAP serialization is portability. The SoapFormatter serializes
objects into SOAP messages or parses SOAP messages and extracts serialized
objects from the message. The following code listing shows how we can
implement serialization using the SOAP protocol.
Listing 4:
public void SOAPSerialize(string filename,Employee employeeObject)
{
FileStream fileStreamObject = new FileStream(filename, FileMode.Create);
SoapFormatter soapFormatter = new SoapFormatter();
soapFormatter.Serialize(fileStreamObject, employeeObject);
fileStreamObject.Close();
}
The following code listing shows how we can implement de-serialization
using the SOAP protocol.
Listing 5:
public static 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;
}