Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
 XML Schema Definition Tool (Xsd.exe...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Tools 
XML Schema Definition Tool (Xsd.exe) 

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.

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

file.extension

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

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.

/P[arameters]:file.xml

Read options for various operation modes from the specified .xml file. The short form is '/p:'. For more information, see the following Remarks section.

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.

/enableDataBinding

Implements the INotifyPropertyChanged interface on all generated types to enable data binding. The short form is '/edb'.

/f[ields]

Generates fields instead of properties. By default, properties are generated.

/l[anguage]:language

Specifies the programming language to use. Choose from CS (C#, which is the 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.

/nologo

Suppresses the banner.

/order

Generates explicit order identifiers on all particle members.

/o[ut]:directoryName

Specifies the output directory to place the files in. The default is the current directory.

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

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.

The following table shows the operations that Xsd.exe performs.

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.

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.

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.

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>

<generateDataSet language='VB' namespace='Microsoft.Serialization.Examples'>

<element>IDItems</element>

</generateDataSet>

</xsd>

Options you can set for the <generateDataSet> element include the following.

Element Description

<element>

Specifies an element in the .xsd file to generate code for.

The following table shows the attributes that can be used with the <generateDataSet> 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).

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.

          

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

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  
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Types to schema      KeithBa ... Thomas Lee   |   Edit   |   Show History
Keep in mind that when you make a schema from a type, you will get a very basic schema - no <choice> or anything like that. You will have to edit the schema to conform to any specific rules you may have in mind beyond simple <sequence>.
Creating Classes and Schema from XML File      Chakravarthy ... SoftwareFDA   |   Edit   |   Show History

This tool is the best utility to create both Class files as well as the Schema files from the XML Files.

All you have to do is the following steps

1. Create the SmpFl.XML File

2. at the command prompt give the command as

 > xsd SmpFl.xml

This will generate the .XSD file with the same name of the file, ie, SmpFl.XSD

3. Using the above .XSD file, you can generate the .CS, ie., the class files with the follwoing code

 > xsd SmpFl.xsd /c 

This will generate the .CS file with the same name of the file, ie, SmpFl.CS, which contains the Class structure. The point to note here is the "/C" option.

(Indeed, as an XML to XSD converter, is this xsd.exe supposed to be Microsoft's replacement for "Microsoft's XSD Inference Tool"? (which used to be available at http://apps.gotdotnet.com/xmltools/xsdinference/ but now (2007.03.06) all http://apps.gotdotnet.com/xmltools is offline.)

Tags What's this?: Add a tag
Flag as ContentBug
Invoking XSD.EXE within Visual Studio (as an External Tool)      SoftwareFDA ... Thomas Lee   |   Edit   |   Show History

xsd.exe can be run within Visual Studio (as an External Tool). Official documentation is needed on how to do this. In the meantime, Google for `xsd.exe "External Tools" ItemDir OR ProjectDir' finds one method: http://developmentnow.com/groups/post.aspx?newsgroupid=54&threadid=741144 says "In VS, Select Tools -> External Tools and press the Add button..." which tells how. I also add, when adding the tool, select "Use Output WIndow" or else the tool's error & warning messages will be lost.

Invoking XSD.EXE within Visual Studio (as an External Tool) Part 2      EdSF ... Thomas Lee   |   Edit   |   Show History

In VS:

  1. Select TOOLS
  2. Select EXTERNAL TOOLS
  3. Select ADD
    • Title = give a meaningful title, like "XSD"
    • Command = C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe
      this is the default install location
    • Arguments = $(ItemPath) /c /l:vb
      use the standard XSD switches you normally would. In the above "/c /l:vb" means create a VB class out of the current XSD document
    • Item Directory = your choice, but if you want the output in the same directory/folder as the xsd file, $(ItemDir)
    • follow the advice above to check "Use Output Window" so you can see what's going on
  4. Click APPLY

After performing the above, you'll see "XSD" (or whatever Title you entered) the next time your select TOOLS. Select it to run xsd.exe on the current document.

Tags What's this?: vs (x) xsd.exe (x) Add a tag
Flag as ContentBug
How create XML      EduValeriano ... Thomas Lee   |   Edit   |   Show History

How can I write XML using created Class?

XSD created a class.vb with my schema. After I alter the class, I need to create a XML file with my information.
How?????

Please and Thanks.

[tfl - 30 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

Another POS Microsoft program      ihatems2   |   Edit   |   Show History
To read a schema it must be pretty simple.
If you have imports, forget using this tool.
Multiple schemas + imports      Royston at i2   |   Edit   |   Show History
If you have imports, you just specify the imported files on the command line.

Bit of a pain to have to do it explicitly, and the documentation doesn't make this at all clear, but it does work well.

Same goes for wsdl.exe - we've been using one WSDL file + 4 imported XSDs since .NET 1.1.

Invoking xsd.exe as part of your build in c#      paquetp   |   Edit   |   Show History
Unload your project, and modify the .csproj file by right clicking it and selectign Edit .csproj.
Add new ItemGroup, and enumerate all your xsd's with your own task type (like, <Xsd>...)
for example:

<Xsd Include="yourschema.xsd">
<SubType>Designer</SubType>
</Xsd>
<Compile Include="yourschema.cs">
<DependentUpon>yourschema.xsd</DependentUpon>
</Compile>

add this after the <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

<Target Name="BeforeBuild" Inputs="@(Xsd)" Outputs="yourschema.cs">
<Exec Command="xsd.exe yourschema.xsd /c /nologo /o:$(ProjectDir)" />
</Target>
<Target Name="AfterClean">
<Delete Files="yourschema.cs" />
</Target>

Now, like magic, the souce is part of your build process. Modify the schema, rebuild, and the source is regenerated.
Tags What's this?: binding (x) msbuild (x) xml (x) xsd (x) Add a tag
Flag as ContentBug
Caution: XSD contains an unresolved bug      KitWest ... Thomas Lee   |   Edit   |   Show History

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98491 describes a bug: "XSD.exe code generation creates invalid code using /classes. Code compiles but generates an error when used for serialization." No indication they plan to fix it.

XSD.exe handling dateTime      Paul S Roberts   |   Edit   |   Show History
When generating classes from a schema with XSD it doesn't seem to handle xs:dateTime correctly

With an xsd snippet
<xs:sequence>
<xs:element name="Date" type="xs:date"/>
<xs:element name="Time" type="xs:time"/>
<xs:element name="DateTime" type="xs:dateTime"/>
</xs:sequence>

The code generated has
[System.Xml.Serialization.XmlElementAttribute(DataType="date")]
public System.DateTime Date {
....
and
[System.Xml.Serialization.XmlElementAttribute(DataType="time")]
public System.DateTime Time {
....
but no serialization attribute on the dateTime element
public System.DateTime DateTime {
get { return this.dateTimeField; }
set { this.dateTimeField = value; }
}

Is this a known problem and is there a workaround ?
Flag as ContentBug
ToString Method      aniruddh_chavan   |   Edit   |   Show History

the code generated by this tool, doesnt generate to string method?
any idea to workaround that

Flag as ContentBug
/out: parameter does not seem to support paths with spaces      PhredTalkPoint   |   Edit   |   Show History
The /out: parameter does not seem to have a way of enclosing the pathname in quotes so that you can specify a path with a space in the name.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker