basic XSD importing and xsd.exe?

5k views Asked by At

I have a main xsd built against another one containing shared types (they are in the same directory).

That's the header of the main one :

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:tns="XMLSchema_Module.xsd"
      xmlns:cs="XML_Common"
      targetNamespace="XMLSchema_Module.xsd"
      elementFormDefault="qualified">

  <xsd:import schemaLocation="XML_Common.xsd" namespace="XML_Common"/>

That's the header of the shared types one :

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      targetNamespace="XML_Common"
      elementFormDefault="qualified">

That's the shared type enumeration I want to use in an attribute of the main schema :

  <xsd:simpleType name="TypesType">
    <xsd:restriction base ="xsd:token">
      <xsd:enumeration value="int"/>

And here is the reference in the main one :

 <xsd:attribute name="Type" type="cs:TypesType"/>

I am using xsd.exe to generate the serialization classes (integrated in the build process as a pre-build event). These xsd were built using VisualStudio.

<Exec Command="&quot;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe&quot; XSchemas\XMLSchema_Module.xsd /c /o:SerializationClasses /n:ScenarioManager" /> 

I am translating the actual message I obtain :

The type XML_Common:TypesType is not declared or is not a simple type.

It seems to be an import problem but I do not know how to solve it.

2

There are 2 answers

0
LB40 On BEST ANSWER

My bad, it seems that xsd.exe does not resolve schema imports. From this article :

Now that the type has been defined in another file, the xsd.exe will generate this error if you attempt to create the create the Request XML: C:\Solution\Project>xsd.exe Request.xsd /c Schema validation warning: Type 'http://www.Example.com/Types:Meeting' is not declared. Warning: Schema could not be validated. Class generation may fail or may produce incorrect results. Error: Error generating classes for schema 'C:\Solution\Projects\Request'.

  • The datatype 'http://www.Example.com/Types:Meeting' is missing. If you would like more help, please type "xsd /?". This is due to the fact that the xsd.exe does not use the schemaLocation hint to find the imported schema. You’ve got to include it as a parameter. in your xsd.exe call: C:\Solution\Project>xsd.exe Types.xsd Request.xsd /c
1
pedimp On

It should be emphasized that despite the pessimistic opening of LB40's answer, its last sentence yields the solution: in your xsd.exe call you have to include the referenced xsd as a parameter before the referencing xsd:

C:\Solution\Project>xsd.exe Types.xsd Request.xsd /c

This will generate a single cs source file containing the classes for both the schemas.