Universe Guide

Home / Facts

Serialization - Soap

SOAP Serialisation is broadly similar to Binary Serialization except the file format is Soap/XML not Binary, goes without saying. There are a few changes, the references and the "using" references for one. You will need to add a reference to System.Runtime.Serialization.Formatters.SOAP.DLL as unlike the other serializations, that one is not automatically referenced.
 using System.Runtime.Serialization.Formatters.Soap; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization; using System.IO;
All classes that are to be serialized must be identified by the attribute [Serializable]. Unlike Binary Serialization, you can not have constructors that take parameters, any attempt at doing so will cause an error. Unlike both XML and Binary Serialization, you need to attribute every class and not just the outer class which you only need to do with XMLSerialization.
// For our example, Date Of Birth is a complex class rather than a Date field. [Serializable] public class clsDOB { public int Day; public int Month; public int Year; } public enum Nationality {British,American,France,Australian,German,Other} [Serializable, XmlRoot(Namespace = "Human")] class clsPerson { // This is not allowed, will cause an error. public clsPerson(string strForename, string strSurname) { Forename = strForename; Surname = strSurname; } // This function will not execute, it will compile but won't execute. [OnDeserialized] public void DisplayName(System.Runtime.Serialization.StreamingContext context) { Console.WriteLine("Hello {0} {1}", Forename, Surname); } public clsPerson() { DOB = new clsDOB(); } public string Forename { get; set; } public string Surname { get; set; } public clsDOB DOB { get; set; } Nationality m_CountryofBirth = Nationality.American ; public Nationality CountryOfBirth { get { return m_CountryofBirth; } set { m_CountryofBirth = value; } } }
The methods [OnSerializing], [onDeserialized] etc will not run in an XML serialization. XML Serialization has attributes of its own to determine how data is stored.

Related Pages of Interest :-

Add a Comment

Email: (Optional)