
Setting Options with an XML File
By using the /parameters switch, you can specify a single XML file that sets various options. The options you can set depend on how you are using the XSD.exe tool. Choices include generating schemas, generating code files, or generating code files that include DataSet features. 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. 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> |
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
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. Type the following at a command prompt (presuming the XML file is named GenerateSchemaFromType.xml):
xsd /p:GenerateSchemaFromType.xml ConsoleApplication1.exe
You must specify only one of the following options for the <generateSchemas> element.
|
Element
|
Description
|
| <assembly> | Specifies an assembly to generate the schema from. |
| <type> | Specifies a type found in an assembly to generate a schema for. |
| <xml> | Specifies an XML file to generate a schema for. |
| <xdr> | Specifies an XDR file to generate a schema for. |
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 --> |
Options you can set for the <generateClasses> element include the following.
|
Element
|
Description
|
| <element> | Specifies an element in the .xsd file to generate code for. |
| <schemaImporterExtensions> | Specifies a type derived from the SchemaImporterExtension class. |
| <schema> | Specifies a XML Schema file to generate code for. Multiple XML Schema files can be specified using multiple <schema> elements. |
The following table shows the attributes that can also be used with the <generateClasses> element.
|
Attribute
|
Description
|
| language | Specifies the programming language to use. Choose from CS (C#, the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). You can also specify a fully qualified name for a class that implements CodeDomProvider. |
| namespace | Specifies the namespace for the generated code. The namespace must conform to CLR standards (for example, no spaces or backslash characters). |
| options | 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. |
You can also control how DataSet code is generated by using the <generateDataSets> element. 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. 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>
Options you can set for the <generateDataSet> element include the following.
|
Element
|
Description
|
| <schema> | Specifies an XML Schema file to generate code for. Multiple XML Schema files can be specified using multiple <schema> elements. |
The following table shows the attributes that can be used with the <generateDataSet> element.
|
Attribute
|
Description
|
| enableLinqDataSet | Specifies that the generated DataSet can be queried against using . The default value is false. |
| language | Specifies the programming language to use. Choose from CS (C#, the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#). You can also specify a fully qualified name for a class that implements CodeDomProvider. |
| namespace | Specifies the namespace for the generated code. The namespace must conform to CLR standards (for example, no spaces or backslash characters). |
There are attributes that you can set on the top level <xsd> element. These options can be used with any of the child elements (<generateSchemas>, <generateClasses> or <generateDataSet>). 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> |
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. |
| nologo | Suppresses the banner. Set to true or false. |
| help | Displays command syntax and options for the tool. Set to true or false. |