The XML Schema Definition Tool and XML Serialization

The XML Schema Definition tool (Xsd.exe) is installed along with the .NET Framework Tools as part of the .NET Framework SDK. The tool is designed primarily for two purposes:

  • To generate either C# or Visual Basic class files that conform to a specific XML Schema definition language (XSD) schema. The tool takes an XML Schema as an argument and outputs a file that contains a number of classes that, when serialized with the XmlSerializer, conform to the schema.
  • To generate an XML Schema document from a .dll file or .exe file. If you need to see the schema of a set of files that you have either created or one that has been modified with attributes, pass the DLL or EXE as an argument to the tool to generate the XML schema.

For details on this tool (and others), see .NET Framework Tools. For details about the tool's options, see XML Schema Definition Tool (Xsd.exe).

To generate classes that conform to a specific schema

  1. Open a Command Prompt window.

  2. Pass the XML Schema as an argument to the XML Schema Definition tool, which creates a set of classes that are precisely matched to the XML Schema. For example:

    xsd mySchema.xsd
    

    The tool can only process schemas that reference the World Wide Web Consortium XML specification of March 16, 2001. In other words, the XML Schema namespace must be "http://www.w3.org/2001/XMLSchema" as shown in the following example.

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
  3. Modify the classes with methods, properties, or fields, as necessary. For more information about modifying a class with attributes, see Controlling XML Serialization Using Attributes and Attributes That Control XML Serialization.

It is often useful to examine the schema of the XML stream that is generated when instances of a class (or classes) are serialized. For example, you might publish your schema for others to use, or you might compare it to a schema with which you are trying to achieve conformity.

To generate an XML Schema Document from a set of classes

  1. Compile the class or classes into a DLL.

  2. Open a Command Prompt window.

  3. Pass the DLL as an argument to Xsd.exe. For example:

    xsd MyFile.dll
    

    The schema (or schemas) will be written, beginning with the name "schema0.xsd".

See Also

Introducing XML Serialization | DataSet | XML Schema Definition Tool (Xsd.exe) | XmlSerializer