Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How do I create classes from an XSD schema?

This topic describes 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 "How Do I Read and Write Objects From XML?" topic to find out how to actually load XML into them, and save XML from them.

  1. Locate an XSD file. In this example, we will use a purchase order schema, as follows:

    PurchaseOrder.XSD
    View Source

  2. 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:
    xsd.exe -c -l:c# -n:XmlSerializationHowTo purchaseorder.xsd
    
  3. The tool uses the XSD file to create a set of classes that can read XML files that use the schema that you specified. Here is the code that gets created for the above schema:

    VB PurchaseOrder.vb
    View Source
    [This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xmlserialization\]
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 when the XML is loaded.

		
[System.Xml.Serialization.XmlArrayItemAttribute("item", IsNullable=false)]
public ItemsItem[] items;
C#

For detailed information on these attributes, see the reference documentation in the SDK. They are located in the System.Xml.Serialization namespace.

Now that you understand how to generate the classes for your XSD schema, you should read the "How Do I Read and Write Objects From XML?" topic to find out how to actually load XML into them, and save XML from them.




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.