Xml Serialization

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Xml Serialization as PDF for free.

More details

  • Words: 511
  • Pages: 4
XML Serialization using C# Introduction Serialization is a process of storing state of an object to some media like disk file or stream. Few weeks back we saw how to serialize objects to disk file in binary format . .NET further provides a way to serialize objects in XML format. Thus objects can be converted into XML documents (serialization) and later on can be constructed back from the XML documents (deserialization). This article demonstrates how to use XML serialization techniques using C#. Namespaces involved Following namespaces are involved in serialization process : • •

System.Xml.Serialization System.IO

Example - XML Serialization and deserialization Following example shows how to serialize objects into XML documents and later read them back. using System; using System.Xml.Serialization; using System.IO; [XmlRoot(ElementName="Employee")] public class Employee { private string m_id; private string m_name; private int m_age; private string[] m_prevcomp; [XmlAttribute(AttributeName="EmpID")] public string EmpID { get{return m_id;} set{m_id=value;} } [XmlElement(ElementName="Name")] public string Name { get{return m_name;} set{m_name=value;} }

[XmlElement(ElementName="Age")] public int Age { get{return m_age;} set{m_age=value;} } [XmlElement(ElementName="PreviousCompany")] public string[] PreviousCompanies { get { return m_prevcomp; } set { }

m_prevcomp=value;

}

public static void Main() { string[] comps={"comp1","comp2"}; Employee e=new Employee(); e.EmpID="EMP1001"; e.Name="bipin"; e.Age=26; e.PreviousCompanies=comps; StreamWriter writer=File.CreateText (@"d:\bipin\vs.net\c#\serialization\sample.xml"); XmlSerializer x=new XmlSerializer(typeof(Employee)); Console.WriteLine("Serialization Start..."); x.Serialize(writer,e); writer.Close(); Console.WriteLine("Serialization Complete..."); Employee e1=new Employee(); StreamReader reader=File.OpenText (@"d:\bipin\vs.net\c#\serialization\sample.xml"); x=new XmlSerializer(typeof(Employee)); Console.WriteLine("Deserialization Start..."); e1=(Employee)x.Deserialize(reader); reader.Close(); Console.WriteLine("Deserialization Complete..."); Console.WriteLine("Deserialized Object Employee Name :" + e1.Name); Console.ReadLine(); } }

We have created Employee class that stores Employee ID, Name, Age and Previous employers in variables m_id, m_name, m_age and m_prevcomp (array) respectively. We have created public properties to encapsulate each f these variables. We want to store the state of an object of Employee class in XML format. The first step is to mark various elements using serialization attributes. Following is a summary of attributes used in previous example : Attribute Purpose This attribute marks the root element of the resulting XML document. XmlRoot(ElementName="Employee") There can be only one root element in an XML document. This attribute marks the property to persist as XmlAttribute(AttributeName="EmpID") XML attribute in the resulting XML document. This attribute marks a property as an XML element. The ElementName property of the attribute is used to XmlElement(ElementName="Name") set the element name in the resulting XML document. It can be any valid XML element name. Once you mark various attributes, you are ready to serialize the class. In our example code from Main method does serialization as follows : StreamWriter writer=File.CreateText (@"d:\bipin\vs.net\c#\serialization\sample.xml"); XmlSerializer x=new XmlSerializer(typeof(Employee)); x.Serialize(writer,e);

First we have created a text file in which our class will be serialized. Next, we have created an instance of XmlSerializer class. Finally, we call Serialize method on this instance passing it the file stream and object instance. Here is how the XML file looks : <Employee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/2001/XMLSchema" EmpID= "EMP1001"> bipin 26 comp1 comp2



You can read the XML file created back into an object like this : StreamReader reader=File.OpenText (@"d:\bipin\vs.net\c#\serialization\sample.xml"); x=new XmlSerializer(typeof(Employee)); e1=(Employee)x.Deserialize(reader);

Simply open the text file to which we previously stored our object and call Deserialize method on the XmlSerializer instance.

Related Documents

Xml Serialization
November 2019 4
Serialization
November 2019 2
Object Serialization
June 2020 4
Xml
June 2020 21
Xml
November 2019 35
Xml
May 2020 25