Friday, March 4, 2011

XML Schema Definition Tool (Xsd.exe)

The XML Schema Definition tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.
xsd file.xdr [/outputdir:directory]
xsd file.xml [/outputdir:directory]
xsd file.xsd {/classes | /dataset} [/element:element]
             [/language:language] [/namespace:namespace]
             [/outputdir:directory] [URI:uri]
xsd {file.dll | file.exe} [/outputdir:directory] [/type:typename [...]]
Argument Description
file.extension The input file to convert. You must specify the extension as one of the following: .xdr, .xml, .xsd, .dll, or .exe. If you specify an XDR schema file (.xdr extension), Xsd.exe converts the XDR schema to an XSD schema. The output file has the same name as the XDR schema, but with the .xsd extension.
If you specify an XML file (.xml extension), Xsd.exe infers a schema from the data in the file and produces an XSD schema. The output file has the same name as the XML file, but with the .xsd extension.
If you specify an XML schema file (.xsd extension), Xsd.exe generates source code for runtime objects that correspond to the XML schema.
If you specify a runtime assembly file (.exe or .dll extension), Xsd.exe generates schemas for one or more types in that assembly. You can use the /type option to specify the types for which to generate schemas. The output schemas are named schema0.xsd, schema1.xsd, and so on. Xsd.exe produces multiple schemas only if the given types specify a namespace using the XMLRoot custom attribute.

General Options

Option Description
/h[elp] Displays command syntax and options for the tool.
/o[utputdir]:directory Specifies the directory for output files. This argument can appear only once. The default is the current directory.
/? Displays command syntax and options for the tool.

XSD File Options

You must specify only one of the following options for .xsd files.
Option Description
/c[lasses] Generates classes that correspond to the specified schema. To read XML data into the object, use the System.XML.Serialization.XMLSerializer.Deserializer method.
/d[ataset] Generates a class derived from DataSet that corresponds to the specified schema. To read XML data into the derived class, use the System.Data.DataSet.ReadXml method.
You can also specify any of the following options for .xsd files.
Option Description
/e[lement]:element Specifies the element in the schema to generate code for. By default all elements are typed. You can specify this argument more than once.
/l[anguage]:language Specifies the programming language to use. Choose from CS (C#; default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). You can also specify a fully qualified name for a class implementing System.CodeDom.Compiler.CodeDomProvider
/n[amespace]:namespace Specifies the runtime namespace for the generated types. The default namespace is Schemas.
/u[ri]:uri Specifies the URI for the elements in the schema to generate code for. This URI, if present, applies to all elements specified with the /element option.

DLL and EXE File Options

Option Description
/t[ype]:typename Specifies the name of the type to create a schema for. You can specify multiple type arguments. If typename does not specify a namespace, Xsd.exe matches all types in the assembly with the specified type. If typename specifies a namespace, only that type is matched. If typename ends with an asterisk character (*), the tool matches all types that start with the string preceding the *. If you omit the /type option, Xsd.exe generates schemas for all types in the assembly.

Remarks

Xsd.exe performs the following operations:
XDR to XSD
Generates an XML schema from an XML-Data-Reduced schema file. XDR is an early XML-based schema format.
XML to XSD
Generates an XML schema from an XML file.
XSD to DataSet
Generates common language runtime DataSet classes from an XSD schema file. The generated classes provide a rich object model for regular XML data.
XSD to Classes
Generates runtime classes from an XSD schema file. The generated classes can be used in conjunction with System.XML.Serialization.XMLSerializer to read and write XML code that follows the schema.
Classes to XSD
Generates an XML schema from a type or types in a runtime assembly file. The generated schema defines the XML format used by System.XML.Serialization.XMLSerializer.
Xsd.exe only allows you to manipulate XML schemas that follow the XML Schema Definition (XSD) language proposed by the World Wide Web Consortium (W3C). For more information on the XML Schema Definition proposal or the XML standard, see http://w3.org.

Examples

The following command generates an XML schema from myFile.xdr and saves it to the current directory.
xsd myFile.xdr 
The following command generates an XML schema from myFile.xml and saves it to the specified directory.
xsd myFile.xml /outputdir:myOutputDir
The following command generates a DataSet that corresponds to the specified schema in the C# language, and saves it as XSDSchemaFile.cs in the current directory.
xsd /dataset /language:CS XSDSchemaFile.xsd
The following command generates XML schemas for all types in the assembly myAssembly.dll and saves them as schema0.xsd in the current directory.
xsd myAssembly.dll  

1 comment: