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 an XML Schema Definition (XSD) File From a Class?

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 serialize an object as XML . Often you will start with an XML Schema Definition (XSD) file, and use the XSD.exe tool to build the classes you save and load from . However, if you started by building your classes first, and then decided to save them as XML, you might 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 need to know to properly read them.

  1. 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. This example will use the following PurchaseOrder type:

    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 to properly read and write 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. Since they affect what XML is used to save the object, they also affect the schema that is generated:

    		
    [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.

  2. 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:
    xsd.exe -t:PurchaseOrder PurchaseOrder.dll
    

  3. 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:

    PurchaseOrder.XSD
    View Source

    Note: The xsd.exe tool generates output schemas named schema0.xsd, schema1.xsd, and so on. For clarity, the output schema has been renamed to PurchaseOrder.XSD in this example.




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