XML Schema Definition Tool (Xsd.exe)XML Schema Definition Tool (Xsd.exe)

XML 架构定义 (Xsd.exe) 工具从 XDR、XML 和 XSD 文件或者从运行时程序集中的类生成 XML 架构或公共语言运行时类。The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

语法Syntax

xsd file.xdr [-outputdir:directory][/parameters:file.xml]
xsd file.xml [-outputdir:directory] [/parameters:file.xml]
xsd file.xsd {/classes | /dataset} [/element:element]
             [/enableLinqDataSet] [/language:language]
                          [/namespace:namespace] [-outputdir:directory] [URI:uri]
                          [/parameters:file.xml]
xsd {file.dll | file.exe} [-outputdir:directory] [/type:typename [...]][/parameters:file.xml]

参数Argument

参数Argument 描述Description
file.extensionfile.extension 指定要转换的输入文件。Specifies the input file to convert. 必须将扩展指定为以下其中一项: xdr、.xml、.xsd、.dll 或 .exe。You must specify the extension as one of the following: .xdr, .xml, .xsd, .dll, or .exe.

如果指定一个 XDR 架构文件(.xdr 扩展名),则 Xsd.exe 将 XDR 架构转换为 XSD 架构。If you specify an XDR schema file (.xdr extension), Xsd.exe converts the XDR schema to an XSD schema. 输出文件与 XDR 架构同名,但扩展名为 .xsd。The output file has the same name as the XDR schema, but with the .xsd extension.

如果指定一个 XML 文件(.xml 扩展名),则 Xsd.exe 从文件中的数据推导出架构并产生一个 XSD 架构。If you specify an XML file (.xml extension), Xsd.exe infers a schema from the data in the file and produces an XSD schema. 输出文件与 XML 文件同名,但扩展名为 .xsd。The output file has the same name as the XML file, but with the .xsd extension.

如果指定一个 XML 架构文件(.xsd 扩展名),则 Xsd.exe 将为对应于 XML 架构的运行时对象生成源代码。If you specify an XML schema file (.xsd extension), Xsd.exe generates source code for runtime objects that correspond to the XML schema.

如果指定一个运行时程序集文件(.exe 或 .dll 扩展名),则 Xsd.exe 为该程序集中的一个或多个类型生成架构。If you specify a runtime assembly file (.exe or .dll extension), Xsd.exe generates schemas for one or more types in that assembly. 可以使用 /type 选项来指定为其生成架构的类型。You can use the /type option to specify the types for which to generate schemas. 输出架构被命名为 schema0.xsd、schema1.xsd,依此类推。The output schemas are named schema0.xsd, schema1.xsd, and so on. 仅当给定类型使用 XMLRoot 自定义特性指定命名空间时,Xsd.exe 才生成多个架构。Xsd.exe produces multiple schemas only if the given types specify a namespace using the XMLRoot custom attribute.

常规选项General Options

选项Option 描述Description
/h [elp ]/h[elp] 显示该工具的命令语法和选项。Displays command syntax and options for the tool.
/o [utputdir ]: 目录/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.
/p [arameters ]: file .xml/p[arameters]:file.xml 从指定的 .xml 文件读取各种操作模式的选项。Read options for various operation modes from the specified .xml file. 缩写形式为 /p:The short form is /p:. 有关详细信息,请参阅 "备注" 部分。For more information, see the Remarks section.

XSD 文件选项XSD File Options

必须为 xsd 文件仅指定下列选项中的一个。You must specify only one of the following options for .xsd files.

选项Option 描述Description
/c [lasses ]/c[lasses] 生成与指定架构相对应的类。Generates classes that correspond to the specified schema. 若要将 XML 数据读入对象中,请使用 XmlSerializer.Deserialize 方法。To read XML data into the object, use the XmlSerializer.Deserialize method.
/d [ataset ]/d[ataset] 生成一个从 DataSet 派生的类,该类与指定的架构相对应。Generates a class derived from DataSet that corresponds to the specified schema. 若要将 XML 数据读入派生类中,请使用 DataSet.ReadXml 方法。To read XML data into the derived class, use the DataSet.ReadXml method.

还可以为 .xsd 文件指定下列任何选项。You can also specify any of the following options for .xsd files.

选项Option 描述Description
/e [lement ]: 元素/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.
/enableDataBinding/enableDataBinding 在所有生成的类型上实现 INotifyPropertyChanged 接口以启用数据绑定。Implements the INotifyPropertyChanged interface on all generated types to enable data binding. 缩写形式为 /edbThe short form is /edb.
/enableLinqDataSet/enableLinqDataSet (短格式: /eld。)指定可以使用 LINQ to DataSet 查询生成的数据集。(Short form: /eld.) Specifies that the generated DataSet can be queried against using LINQ to DataSet. 此选项在同时指定 /dataset 选项的情况下使用。This option is used when the /dataset option is also specified. 有关详细信息,请参阅 LINQ to DataSet 概述查询类型化数据集For more information, see LINQ to DataSet Overview and Querying Typed DataSets. 有关使用 LINQ 的一般信息,请参阅语言集成查询(linq C# )语言集成查询(linq)-Visual BasicFor general information about using LINQ, see Language-Integrated Query (LINQ) - C# or Language-Integrated Query (LINQ) - Visual Basic.
/f [ields ]/f[ields] 生成字段,而不是生成属性。Generates fields instead of properties. 默认情况下生成属性。By default, properties are generated.
/l [anguage ]: language/l[anguage]:language 指定要使用的编程语言。Specifies the programming language to use. CS(默认情况下为 C#)、VB (Visual Basic)、JS (JScript) 或 VJS (Visual J#) 中进行选择。Choose from CS (C#, which is the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). 也可指定实现 System.CodeDom.Compiler.CodeDomProvider 的类的完全限定名You can also specify a fully qualified name for a class implementing System.CodeDom.Compiler.CodeDomProvider
/n [amespace ]: 命名空间/n[amespace]:namespace 为生成的类型指定运行时命名空间。Specifies the runtime namespace for the generated types. 默认命名空间为 SchemasThe default namespace is Schemas.
/nologo/nologo 取消显示版权标志。Suppresses the banner.
/order/order 在所有粒子成员上生成显式顺序标识符。Generates explicit order identifiers on all particle members.
/o [ut ]: directoryName/o[ut]:directoryName 指定用来放置文件的输出目录。Specifies the output directory to place the files in. 默认为当前目录。The default is the current directory.
/u [ri ]: uri/u[ri]:uri 为架构中要为其生成代码的元素指定 URI。Specifies the URI for the elements in the schema to generate code for. 该 URI(如果存在)应用于使用 /element 选项指定的所有元素。This URI, if present, applies to all elements specified with the /element option.

DLL 和 EXE 文件选项DLL and EXE File Options

选项Option 描述Description
/t [ype ]: typename/t[ype]:typename 指定要为其创建架构的类型的名称。Specifies the name of the type to create a schema for. 可以指定多个类型参数。You can specify multiple type arguments. 如果 typename 不指定一个命名空间,则 Xsd.exe 将程序集中的所有类型与指定类型相匹配。If typename does not specify a namespace, Xsd.exe matches all types in the assembly with the specified type. 如果 typename 指定一个命名空间,则仅匹配那个类型。If typename specifies a namespace, only that type is matched. 如果 typename 以星号字符 (*) 结尾,则此工具匹配所有以 * 前的字符串开头的类型。If typename ends with an asterisk character (*), the tool matches all types that start with the string preceding the *. 如果省略 /type 选项,则 Xsd.exe 为程序集中的所有类型生成架构。If you omit the /type option, Xsd.exe generates schemas for all types in the assembly.

备注Remarks

下表显示了 Xsd.exe 执行的操作。The following table shows the operations that Xsd.exe performs.

XDR 到 XSDXDR to XSD 使用精简 XML 数据架构文件生成 XML 架构。Generates an XML schema from an XML-Data-Reduced schema file. XDR 为早期基于 XML 的架构格式。XDR is an early XML-based schema format.
XML 到 XSDXML to XSD 使用 XML 文件生成 XML 架构。Generates an XML schema from an XML file.
XSD 到 DataSetXSD to DataSet 使用 XSD 架构文件生成公共语言运行时 DataSet 类。Generates common language runtime DataSet classes from an XSD schema file. 生成的类为规则 XML 数据提供复杂对象模型。The generated classes provide a rich object model for regular XML data.
XSD 到类XSD to Classes 使用 XSD 架构文件生成运行时类。Generates runtime classes from an XSD schema file. 生成的类可以与 System.Xml.Serialization.XmlSerializer 一起使用,来读写遵循该架构的 XML 代码。The generated classes can be used in conjunction with System.Xml.Serialization.XmlSerializer to read and write XML code that follows the schema.
类到 XSDClasses to XSD 使用运行时程序集文件中的一个或多个类型生成 XML 架构。Generates an XML schema from a type or types in a runtime assembly file. 生成的架构定义 XmlSerializer 使用的 XML 格式。The generated schema defines the XML format used by the XmlSerializer.

Xsd.exe 只允许操作遵循由万维网联合会 (W3C) 提议的 XML 架构定义 (XSD) 语言的 XML 架构。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). 有关 XML 架构定义提议或 XML 标准的详细信息,请参阅 https://w3.orgFor more information on the XML Schema Definition proposal or the XML standard, see https://w3.org.

通过 XML 文件设置选项Setting Options with an XML File

通过使用 /parameters 开关,可指定设置各种选项的单个 XML 文件。By using the /parameters switch, you can specify a single XML file that sets various options. 可设置的选项取决于您如何使用 XSD.exe 工具。The options you can set depend on how you are using the XSD.exe tool. 选择包括生成架构、生成代码文件,或生成包含 DataSet 功能的代码文件。Choices include generating schemas, generating code files, or generating code files that include DataSet features. 例如,生成架构时可将 <assembly> 元素设置为可执行文件 (.exe) 或类库文件 (.dll) 的名称,但生成代码文件时则不能。For example, you can set the <assembly> element to the name of an executable (.exe) or type library (.dll) file when generating a schema, but not when generating a code file. 下面的 XML 演示如何将 <generateSchemas> 元素用于指定的可执行文件:The following XML shows how to use the <generateSchemas> element with a specified executable:

<!-- This is in a file named GenerateSchemas.xml. -->
<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateSchemas>
   <assembly>ConsoleApplication1.exe</assembly>
</generateSchemas>
</xsd>

如果前面的 XML 包含在名为 GenerateSchemas.xml 的文件中,则通过在命令提示处键入下面的内容并按 Enter 使用 /parameters 开关:If the preceding XML is contained in a file named GenerateSchemas.xml, then use the /parameters switch by typing the following at a command prompt and pressing ENTER:

 xsd /p:GenerateSchemas.xml

另外,如果为程序集中的单个类型生成架构,则可以使用下面的 XML:On the other hand, if you are generating a schema for a single type found in the assembly, you can use the following XML:

<!-- This is in a file named GenerateSchemaFromType.xml. -->
<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateSchemas>
   <type>IDItems</type>
</generateSchemas>
</xsd>

但是若要使用上面的代码,您还必须在命令提示处提供程序集的名称。But to use preceding code, you must also supply the name of the assembly at the command prompt. 在命令提示处键入下面的内容(假设 XML 文件名为 GenerateSchemaFromType.xml):Type the following at a command prompt (presuming the XML file is named GenerateSchemaFromType.xml):

xsd /p:GenerateSchemaFromType.xml ConsoleApplication1.exe

必须为 <generateSchemas> 元素仅指定以下选项中的一个。You must specify only one of the following options for the <generateSchemas> element.

元素Element 描述Description
<assembly><assembly> 指定将从中生成架构的程序集。Specifies an assembly to generate the schema from.
<type><type> 指定程序集中找到的要为其生成架构的类型。Specifies a type found in an assembly to generate a schema for.
<xml><xml> 指定要为其生成架构的 XML 文件。Specifies an XML file to generate a schema for.
<xdr><xdr> 指定要为其生成架构的 XDR 文件。Specifies an XDR file to generate a schema for.

若要生成代码文件,请使用 <generateClasses> 元素。To generate a code file, use the <generateClasses> element. 下面的示例生成一个代码文件。The following example generates a code file. 请注意,另外还显示了两个特性,它们允许您为生成的文件设置编程语言和命名空间。Note that two attributes are also shown that allow you to set the programming language and namespace of the generated file.

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='VB' namespace='Microsoft.Serialization.Examples'/>
</xsd>
<!-- You must supply an .xsd file when typing in the command line.-->
<!-- For example: xsd /p:genClasses mySchema.xsd -->

可为 <generateClasses> 元素设置以下选项。Options you can set for the <generateClasses> element include the following.

元素Element 描述Description
<element><element> 指定 .xsd 文件中要为其生成代码的元素。Specifies an element in the .xsd file to generate code for.
<schemaImporterExtensions><schemaImporterExtensions> 指定派生自 SchemaImporterExtension 类的类型。Specifies a type derived from the SchemaImporterExtension class.
<schema><schema> 指定要为其生成代码的 XML 架构文件。Specifies a XML Schema file to generate code for. 可使用多个 <schema> 元素指定多个 XML 架构文件。Multiple XML Schema files can be specified using multiple <schema> elements.

下表显示也可用于 <generateClasses> 元素的特性。The following table shows the attributes that can also be used with the <generateClasses> element.

特性Attribute 描述Description
语言language 指定要使用的编程语言。Specifies the programming language to use. CS(默认情况下为 C#)、VB (Visual Basic)、JS (JScript) 或 VJS (Visual J#) 中进行选择。Choose from CS (C#, the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). 也可指定实现 CodeDomProvider 的类的完全限定名。You can also specify a fully qualified name for a class that implements CodeDomProvider.
namespacenamespace 为生成的代码指定命名空间。Specifies the namespace for the generated code. 命名空间必须符合 CLR 标准(例如,没有空格或反斜杠字符)。The namespace must conform to CLR standards (for example, no spaces or backslash characters).
选项options 以下值之一:noneproperties(生成属性而不是公共字段)、orderenableDataBinding(请参见前面“XSD 文件选项”一节的 /order/enableDataBinding 开关)。One of the following values: none, properties (generates properties instead of public fields), order, or enableDataBinding (see the /order and /enableDataBinding switches in the preceding XSD File Options section.

使用 DataSet 元素还可以控制如何生成 <generateDataSet> 代码。You can also control how DataSet code is generated by using the <generateDataSet> element. 下面的 XML 指定生成的代码使用 DataSet 结构(如 DataTable 类)为指定元素创建 Visual Basic 代码。The following XML specifies that the generated code uses DataSet structures (such as the DataTable class) to create Visual Basic code for a specified element. 生成的数据集结构将支持 LINQ 查询。The generated DataSet structures will support LINQ queries.

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
    <generateDataSet language='VB' namespace='Microsoft.Serialization.Examples' enableLinqDataSet='true'>
    </generateDataSet>
</xsd>

可为 <generateDataSet> 元素设置以下选项。Options you can set for the <generateDataSet> element include the following.

元素Element 描述Description
<schema><schema> 指定要为其生成代码的 XML 架构文件。Specifies an XML Schema file to generate code for. 可使用多个 <schema> 元素指定多个 XML 架构文件。Multiple XML Schema files can be specified using multiple <schema> elements.

下表显示可与 <generateDataSet> 元素一起使用的特性。The following table shows the attributes that can be used with the <generateDataSet> element.

特性Attribute 描述Description
enableLinqDataSetenableLinqDataSet 指定可使用 LINQ to DataSet 查询的生成的数据集。Specifies that the generated DataSet can be queried against using LINQ to DataSet. 默认值为 False。The default value is false.
语言language 指定要使用的编程语言。Specifies the programming language to use. CS(默认情况下为 C#)、VB (Visual Basic)、JS (JScript) 或 VJS (Visual J#) 中进行选择。Choose from CS (C#, the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). 也可指定实现 CodeDomProvider 的类的完全限定名。You can also specify a fully qualified name for a class that implements CodeDomProvider.
namespacenamespace 为生成的代码指定命名空间。Specifies the namespace for the generated code. 命名空间必须符合 CLR 标准(例如,没有空格或反斜杠字符)。The namespace must conform to CLR standards (for example, no spaces or backslash characters).

有些特性可在顶级 <xsd> 元素上设置。There are attributes that you can set on the top level <xsd> element. 这些选项可用于任何子元素(<generateSchemas><generateClasses><generateDataSet>)。These options can be used with any of the child elements (<generateSchemas>, <generateClasses> or <generateDataSet>). 下面的 XML 代码在名为“MyOutputDirectory”的输出目录中为名为“IDItems”的元素生成代码。The following XML code generates code for an element named "IDItems" in the output directory named "MyOutputDirectory".

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/' output='MyOutputDirectory'>
<generateClasses>
    <element>IDItems</element>
</generateClasses>
</xsd>

下表显示也可用于 <xsd> 元素的特性。The following table shows the attributes that can also be used with the <xsd> element.

特性Attribute 描述Description
输出output 将放置生成的架构或代码文件的目录的名称。The name of a directory where the generated schema or code file will be placed.
nologonologo 取消显示版权标志。Suppresses the banner. 设置为 truefalseSet to true or false.
帮助help 显示该工具的命令语法和选项。Displays command syntax and options for the tool. 设置为 truefalseSet to true or false.

示例Examples

下面的命令从 myFile.xdr 生成一个 XML 架构并将它保存到当前目录中。The following command generates an XML schema from myFile.xdr and saves it to the current directory.

xsd myFile.xdr

下面的命令从 myFile.xml 生成一个 XML 架构并将它保存到指定目录中。The following command generates an XML schema from myFile.xml and saves it to the specified directory.

xsd myFile.xml /outputdir:myOutputDir

下面的命令生成一个与 C# 语言中的指定架构相对应的数据集,并在当前目录中将其保存为 XSDSchemaFile.csThe following command generates a data set 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

下面的命令为程序集 myAssembly.dll 中的所有类型生成 XML 架构,并在当前目录中将它们保存为 schema0.xsdThe 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

请参阅See also