XML Serialization and De-Serialization
Serialization is the process of converting specific data to a format that can be stored or transmitted, and later re-used to retrieve the information in its initial state.
The deserialization process goes the other way around – it converts the bytes of data obtained from a specific source to a workable object or data unit. We will load in an XML, then pass the data to the deserializer and it will produce an instance of the class populated with the data.
XML Serialization is the process of saving class member data into an XML. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability.
Here we have a class call “XMLUtility”, which serialize and deserialize the xml to object and vice-versa.
XMLUtility.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Xml.Serialization; using System.Xml; namespace XMLSerialization { public class XMLUtility { public string CreateXML(Object YourClassObject) { XmlDocument xmlDoc = new XmlDocument(); //Represents an XML document, // Initializes a new instance of the XmlDocument class. XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType()); // Creates a stream whose backing store is memory. using (MemoryStream xmlStream = new MemoryStream()) { xmlSerializer.Serialize(xmlStream, YourClassObject); xmlStream.Position = 0; //Loads the XML document from the specified string. xmlDoc.Load(xmlStream); return xmlDoc.InnerXml; } } public Object CreateObject(string XMLString,Object YourClassObject) { XmlSerializer oXmlSerializer = new XmlSerializer(YourClassObject.GetType()); //The StringReader will be the stream holder for the existing XML file YourClassObject = oXmlSerializer.Deserialize(new StringReader(XMLString)); //initially deserialized, the data is represented by an object without a defined type return YourClassObject; } } }
On the main page we need to create an object of XMLUtility. Using this object we can call “CreateXML()” and “CreateObject()” methods. Here is a bit of example to demonstrate how it works:
Main Page:
///// XML Serialization and De-Serialization // Example 1: Convert Single object to xml //// Testdata obj = new Testdata(); obj.name = "Shiwa"; obj.address = "B-48, Aliganj"; obj.age = 25; obj.isStudent = false; XMLUtility objXml = new XMLUtility(); string xml = objXml.CreateXML(obj); Response.Write(xml); /// here xml variable contains the converted xml string using object. /* Output is: * <?xml version="1.0"?><Testdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> * <name>Shiwa</name><address>B-48, Aliganj</address><age>25</age><isStudent>false</isStudent> * </Testdata> */ Testdata obj3 = new Testdata(); Testdata obj2 =(Testdata) objXml.CreateObject(xml, obj3); //here obj2 object holds the values from xml string. ///// // Example 2: Convert List of object to xml string and vice-versa. ///// Listlst = new List (); for (int i = 1; i < 4; i++) { var objTest = new Testdata(); objTest.name = "Emp"+i.ToString(); objTest.address = "B-48, Aliganj"; objTest.age = i * 10; objTest.isStudent = (i % 2 == 0 ? true : false); lst.Add(objTest); } string xmlStr = objXml.CreateXML(lst); Response.Write(xmlStr); /* Output is: * <?xml version="1.0"?><ArrayOfTestdata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> * <Testdata><name>Emp1</name><address>B-48, Aliganj</address><age>10</age><isStudent>false</isStudent></Testdata> * <Testdata><name>Emp2</name><address>B-48, Aliganj</address><age>20</age><isStudent>true</isStudent></Testdata> * <Testdata><name>Emp3</name><address>B-48, Aliganj</address><age>30</age><isStudent>false</isStudent></Testdata> * </ArrayOfTestdata> */