The XML Schema Definition Tool (xsd.exe) can be used to generate xml schema files from XML and better still C# classes from xml schema files.
Creating classes based upon an XML schema file
So in it’s simplest usage we can simply type
xsd person.xsd /classes
and this generates C# classes representing the xml schema. The default output is C# but using the /language or the shorter form /l switch we can generate Visual Basic using the VB value, JScript using JS or CS if we wanted to explicitly static the language was to be C#. So for example using the previous command line but now to generate VB code we can write
xsd person.xsd /classes /l:VB
Assuming we have an xml schema, person.xsd, which looks like this
<?xml version="1.0" encoding="utf-8"?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person" nillable="true" type="Person" /> <xs:complexType name="Person"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="FirstName" type="xs:string" /> <xs:element minOccurs="0" maxOccurs="1" name="LastName" type="xs:string" /> <xs:element minOccurs="1" maxOccurs="1" name="Age" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:schema>
The class created (in C#) looks like the following (comments removed)
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)] public partial class Person { private string firstNameField; private string lastNameField; private int ageField; public string FirstName { get { return this.firstNameField; } set { this.firstNameField = value; } } public string LastName { get { return this.lastNameField; } set { this.lastNameField = value; } } public int Age { get { return this.ageField; } set { this.ageField = value; } } }
Creating an XML schema based on an XML file
It might be that we’ve got an XML file but no xml schema, so we’ll need to convert that to an xml schema before we can generate our classes file. Again we can use xsd.exe
xsd person.xml
the above will create an xml schema based upon the XML file, obviously this is limited to what is available in the XML file itself, so if your XML doesn’t have “optional” elements/attributes xsd.exe obviously cannot include those in the schema it produces.
Assuming we therefore started with an XML file, the person.xml, which looks like the following
<?xml version="1.0" encoding="utf-8"?> <Person> <FirstName>Spoungebob</FirstName> <LastName>Squarepants</LastName> <Age>21</Age> </Person>
Note: I’ve no idea if that is really SpongeBob’s age.
Running xsd.exe against person.xml file we get the following xsd schema
<?xml version="1.0" encoding="utf-8"?> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="FirstName" type="xs:string" minOccurs="0" /> <xs:element name="LastName" type="xs:string" minOccurs="0" /> <xs:element name="Age" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="Person" /> </xs:choice> </xs:complexType> </xs:element> </xs:schema>
From this we could now create our classes as previously outlined.
Creating an xml schema based on .NET type
What if we’ve got a class/type and we want to serialize it as XML, let’s use xsd.exe to create the XML schema for us.
If the class looks like the following
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } [code] <em>Note: Assuming the class is compiled into an assembly call DomainObjects.dll</em> Then running xsd.exe with the following command line [code language="xml"] xsd.exe DomainObjects.dll /type:Person
will then generate the following xml schema
<?xml version="1.0" encoding="utf-8"?> <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person" nillable="true" type="Person" /> <xs:complexType name="Person"> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" name="FirstName" type="xs:string" /> <xs:element minOccurs="0" maxOccurs="1" name="LastName" type="xs:string" /> <xs:element minOccurs="1" maxOccurs="1" name="Age" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:schema>
You’ll notice this is slightly different from the code generated from the person.xml file.