Showing posts with label BizTalk. Show all posts
Showing posts with label BizTalk. Show all posts

Monday, July 9, 2007

Generate .NET classes with XSD.EXE based on schemas with import declarations

If you define two schemas - MySchema and MySubSchema - where MySchema references MySubschema, you will probably get in trouble if you try to generate .NET classes for those schemas using XSD.EXE.

Here's a quick example: MySchema.xsd is imports MySubschema.xsd and declares an element of type ns2:MySubSchemaRoot

MySchema.xsd

<?xml version="1.0" encoding="utf-16"?>

<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://Namespace1" xmlns:ns2="http://Namespace2" targetNamespace="http://Namespace1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:import schemaLocation=".\MySubSchema.xsd" namespace="http://Namespace2" />


<xs:annotation>

<xs:appinfo>

<b:references>

<b:reference targetNamespace="http://Namespace2" />

</b:references>

</xs:appinfo>

</xs:annotation>

<xs:element name="MySchemaRoot">

<xs:complexType>

<xs:sequence>

<xs:element name="Element1" type="xs:string" />


<xs:element ref="ns2:MySubSchemaRoot" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>


 

MySubSchema.xsd

<?xml version="1.0" encoding="utf-16"?>

<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://Namespace2" targetNamespace="http://Namespace2" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="MySubSchemaRoot">

<xs:complexType>

<xs:sequence>

<xs:element name="Element1" type="xs:string" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>


 

If you run XSD.EXE to generate the .NET class for MySchema…

xsd.exe /c MySchema.xsd

…you'll get this error:

The element 'http://Namespace2:MySubSchemaRoot' is missing.

To workaround this issue, you must also specify MySubSchema.xsd as a parameter for xsd.exe, like this:

xsd.exe /c MySubSchema.xsd MySchema.xsd

This will correctly generate MySchema.cs class