<h4>How do I create an XML Schema Definition (XSD) File From a Class?</h4>
This topic describes how to build an XSD file for classes that you save and load from XML using the XML Serialization classes. The XML Serialization classes allow you to <a href="RWObjFromXML.aspx">serialize an object as XML </a>. Often you'll start with an XML Schema Definition (XSD) file, and <a href="XsdToCls.aspx">use the XSD.exe tool to build the classes you save and load from </a>. However, if you started by building your classes first, and then decided to save them as XML, you may want to generate an XML Schema Definition (XSD) file that describes the format that the serializer will use for your objects. This file provides the information that consumers of your persisted classes would need to know to properly read them. <p>
<ol>
<li>Locate the assembly and class you want to export. You only need to know the name of the class that corresponds to the root element of the xml document. In this example, we will use the following PurchaseOrder type:<p>
<Acme:SourceRef id="RefCode" runat="server" />
<p>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. Since they affect what XML is used to save the object, they also affect the schema that is generated<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>
<p>
<li> Use the xsd.exe tool in the SDK to generate an XSD schema from the class and all classes used by that class. The ût switch tells the program only to generate a schema for one particular type in the test.exe file:
<div class="code"><xmp>
xsd.exe -t:PurchaseOrder PurchaseOrder.dll
</xmp></div>
<li> The result is an XSD schema that describes what the XML file will look like when you serialize your object using the XML Serialization classes:<p>