Friday, April 13, 2007

* Tool : XML Schema Definition Tool (Xsd.exe)

XSD.exe which comes as part of .net framework is mainly used to generate XML schemas or .net classes based on XML schema.

Some of the generations possible with this tool are :

  • XDR -> XSD : generates xml schema based on xml data reduced schema
  • XML -> XSD : generates xml schema based on xml data file
  • XSD -> Dataset : generates code of dataset based class which can be used to read xml data compliant to input xsd using System.Data.DataSet.ReadXml
  • XSD -> Classes : generates code of classes which can be used to read and write xml data using System.Xml.Serialization.XmlSerializer
  • Classes -> XSD : generates xml schema for types in a runtime assembly

 Lets create a sample xml data file and work on it using xsd tool.

Consider the following xml data file EmployeeList.xml:

<EmployeeList>
<Company ID="1">
<Employee>Employee11</Employee>
<Employee>Employee12</Employee>
<Employee>Employee13</Employee>
</Company>
<Company ID="2">
<Employee>Employee21</Employee>
<Employee>Employee22</Employee>
<Employee>Employee23</Employee>
</Company>
<Company ID="3">
<Employee>Employee31</Employee>
<Employee>Employee32</Employee>
<Employee>Employee33</Employee>
<Employee>Employee34</Employee>
</Company>
</EmployeeList>


Execute XSD.exe on the xml data file:



> XSD EmployeeList.xml



The resultant xsd file EmployeeList.xsd will be :

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="EmployeeList" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="EmployeeList" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Company">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" nillable="true" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="Employee_Text" msdata:Ordinal="0">
<xs:extension base="xs:string">
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>


Now we can generate code of classes using the same tool and xsd generated which can be used to read and write xml data using System.Xml.Serialization.XmlSerializer.



>xsd EmployeeList.xsd /classes



The code generated is :

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmployeeList {

private EmployeeListCompany[] itemsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Company", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public EmployeeListCompany[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmployeeListCompany {

private EmployeeListCompanyEmployee[] employeeField;

private string idField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Employee", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public EmployeeListCompanyEmployee[] Employee {
get {
return this.employeeField;
}
set {
this.employeeField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EmployeeListCompanyEmployee {

private string valueField;

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}


The sample usage of this class can be as below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace XSD_TOOL_SAMPLE
{
class Program
{
static void Main(string[] args)
{
EmployeeList empList;
XmlSerializer xs = new XmlSerializer(typeof(EmployeeList));
FileStream fs = new FileStream(@"D:\Vikas\vikas\tech\code\CSharp\XML\EmployeeList.xml",FileMode.Open);
empList = (EmployeeList)xs.Deserialize(fs);
Console.WriteLine(empList.Items[0].Employee[0].Value);
}
}
}


The output for this will be the value of first Employee element.



Output : Employee11



More detailed documentation on xsd tool can be found on msdn.



Other Posts



2 comments:

  1. Nice simple example. Thanks.

    ReplyDelete
  2. Now how would I directly get the first employee from Company with id="2"? (without using Items[0] but something like Company[id="0"])

    ReplyDelete