Universe Guide
HomeAliensConstellationsTelevision and Films ListFact ListGames ListWarcraftSearchTwitterFacebook

Serialization - XML

XML Serialisation is broadly similar to Binary Serialization except the file format is XML not Binary, goes without saying. There are a few changes, the references and using references for one. :-
 using System.Runtime.Serialization; using System.Xml.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.
// For our example, Date Of Birth is a complex class rather than a Date field. public class clsDOB { public int Day; public int Month; public int Year; } public enum Nationality { [XmlEnum(Name="British")] British, [XmlEnum(Name = "American")] American, [XmlEnum(Name="French")] France, [XmlEnum(Name="Australian")] Australian, [XmlEnum(Name="German")] German, [XmlEnum(Name="Other")] 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); }
[XmlElement] public string Forename { get; set; } [XmlAttribute] public string Surname { get; set; } // Must be an XML Element so that it can be properly serialized. [XmlElement] public clsDOB DOB { get; set; } [XmlEnum] Nationality m_CountryofBirth = Nationality.American ; [XmlElement] 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.

QualifierExplanation
[XMLAttribute]The value will be an attribute on the "first" line. Only simple data types can be Attributes, properties that are a class must be elements.
[XMLElement]The item will be serialised to an XML Element, these can be either simple type or class object types.
[XMLEnum]Prodives the Element Name for an Enumeration Member of the object.
[XmlRoot]This names the XML.
[XmlText]Item will be serialized as XML Text
[XmlType]Name and namespace of the XML namespace.


Now comes the fun part, the serialization which is really easy. In short, you define the formatter then write the object to a stream. If you looked at the file that is created, you will find the information is relatively easy to understand.

 class Program { static void Main(string[] args) { //Create Instance of object to be serialized. clsPerson Person = new clsPerson(); Person.Forename = "Mister"; Person.Surname = "Developer"; Person.DOB.Day = 25; Person.DOB.Month = 1; Person.DOB.Year = 2011; // Create serializer that will do the work. XmlSerializer binFormat = new XmlSerializer(typeof (clsPerson)); // Open a stream where the serialized file will be stored. using (Stream fptr = new FileStream("C:\\temp\\user.dat", FileMode.Create, FileAccess.Write, FileShare.None)) { binFormat.Serialize(fptr, Person); } Person = null; // Deserialize the object. using (Stream fptr = File.OpenRead("c:\\temp\\user.dat")) { // As we have an [OnDeserialized] function, the forename + surname will be displayed on screen. Person = (clsPerson)binFormat.Deserialize(fptr); Console.WriteLine("Hello {0} {1} who is {2}", Person.Forename, Person.Surname, Person.CountryOfBirth.ToString()); } Console.ReadKey(); } }
You will be able to read the user.dat file that is created. The above example will create the following xml file.
<?xml version="1.0"?> <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Surname="Developer" xmlns="Human"> <Forename>Mister</Forename> <DOB> <Day>25</Day> <Month>1</Month> <Year>2011</Year> </DOB> <CountryOfBirth>American</CountryOfBirth> </clsPerson>

Related Pages of Interest :-



Add a Comment


Name:
Email: (Optional)
Comment: