<h4>How do I create classes from an XSD schema?</h4>
This topic will describe how to create classes that can be used to read and write XML that is defined by an XML Schema Description (XSD) file. Once you understand how to generate the classes for your XSD schema, you should read the <a href="RWObjFromXML.aspx">"How Do I Read and Write Objects From XML?"</a> topic to find out how to actually load XML into them, and save XML from them.
<ol>
<li> Locate an XSD file. In this example, we will use a purchase order schema, as follows: <p>
<Acme:SourceRef id="Ref" runat="server" />
<p>
<li> Use the xsd.exe tool in the SDK to generate source code for the classes you want. Do this by specifying the name of the XSD file as an argument to the executable. The example below creates classes in C# using the purchaseorder.xsd schema and puts them in the XmlSerializationHowTo namespace:
<li> The tool uses the xsd file to create a set of classes that can read XML files that use the schema you specified. Here is the code that gets created for the above schema:<p>
<Acme:SourceRef id="RefCode" runat="server" />
</ol>
In the code you will notice additional "Custom Metadata Attributes" above the field declarations. These are used to guide the Xml Serializer classes in properly reading and writing the XML. They are basically "hints" to the parser. For example, the Items field has a custom attribute applied to it to tell the serializer to that the Items field should contain an array of item objects which get created from the xml being loaded.<p>
<div class="code"><xmp>
[XmlArray("items"), XmlArrayItem("item")]
public Item[] Items;
</xmp></div>
<p>
For detailed information on these attributes, see the reference documentation in the SDK help. They are located in the System.Xml.Serialization namespace. <p>
Now that you understand how to generate the classes for your XSD schema, you should read the <a href="RWObjFromXML.aspx">"How Do I Read and Write Objects From XML?"</a> topic to find out how to actually load XML into them, and save XML from them.