Introduction
Saving XML to a class object gives greater flexibility and maintainability. Moreover, it�s the preferred way of returning data from web services as compared to using DataSet
s. Saving XML to a class object is called serialization and reading back using the reverse process is named as deserialization.
Process\sample
Let's take an example of an XSD named Emp.XSD:
="1.0" ="UTF-8"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Emp">
<xs:complexType>
<xs:sequence>
<xs:element name="EmpInfo" type="EmpInfo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="EmpInfo">
<xs:sequence>
<xs:element name="Code" type="requiredString"/>
<xs:element name="FirstName" type="requiredString"/>
<xs:element name="LastName" type="requiredString"/>
<xs:element name="Destination" type="requiredString"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="requiredString">
<xs:annotation>
<xs:documentation>template for required strings</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="optionalString">
<xs:annotation>
<xs:documentation>template for optional strings</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Now, to convert a class from XSD, we would run the following command:
xsd C:\Emp.xsd /t:lib /l:cs /c
from the Visual Studio .NET 2003 command prompt. This would create a class file named Emp
.
EMP Class
using System.Xml.Serialization;
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class Emp {
public EmpInfo EmpInfo;
}
public class EmpInfo {
public string Code;
public string FirstName;
public string LastName;
public string Destination;
}
Now, we could save an employee XML data to this class using the function:
public Emp EmpObject(string xml)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Emp));
stream = new StringReader(xml);
reader = new XmlTextReader(stream);
return (Emp)serializer.Deserialize(reader);
}
catch
{
return null;
}
finally
{
if(stream != null) stream.Close();
if(reader != null) reader.Close();
}
}
And to get the XML based on the Emp
class object, use the following function:
public string EmpXml(Emp emp)
{
MemoryStream stream = null;
TextWriter writer = null;
try
{
stream = new MemoryStream();
writer = new StreamWriter(stream, Encoding.Unicode) ;
XmlSerializer serializer = new XmlSerializer(typeof(Emp));
serializer.Serialize(writer, emp);
int count = (int) stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch
{
return string.Empty;
}
finally
{
if(stream != null) stream.Close();
if(writer != null) writer.Close();
}
}