XML Serialization
XML Serialization
Introduction
Namespaces involved
• System.Xml.Serialization
• 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;
}
}
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...");
}
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 :
<?xml version="1.0" encoding="utf-8"?>
<Employee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EmpID=
"EMP1001">
<Name>bipin</Name>
<Age>26</Age>
<PreviousCompany>comp1</PreviousCompany>
<PreviousCompany>comp2</PreviousCompany>
</Employee>
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.