Xsd.exe is unable generate class when Importing ComplexType
Asked Answered
F

2

8

I am trying to run Xsd.exe on a XSD file and I am getting following error. I am using IMPORT because host-namespace is different from foreign-namespace.

A2.xsd depends on A21.xsd, which in turn depends on A22.xsd (all are in same folder)

ERROR: "The datatype 'http://service.a1.com/base1/2005/:EmployeeDefinition' is missing"

xsd.exe /classes /out:C:\Temp\ "C:\Temp\A2.xsd" /language:CS

A2.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service.a1.com/base/2005/" xmlns:c1="http://service.a1.com/base1/2005/" elementFormDefault="qualified">
  <xs:import namespace="http://service.a1.com/base1/2005/" schemaLocation="a21.xsd"/> 
  <xs:element name="Employee" nillable="true" type="c1:EmployeeDefinition" />
</xs:schema>

A21,xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service.a1.com/base1/2005/" xmlns:n2="http://service.a1.com/base2/2005/" elementFormDefault="qualified">
 <xs:import namespace="http://service.a1.com/base2/2005/" schemaLocation="a22.xsd"/> 
 <xs:complexType  name="EmployeeDefinition">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="EmployeeID" type="xs:int" />
      <xs:element minOccurs="0" maxOccurs="1" name="FirstName" type="xs:string" />
      <xs:element name="Address" nillable="true" type="n2:AddressDefinition" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

A22.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service.a1.com/base2/2005/" elementFormDefault="qualified">
  <xs:complexType name="AddressDefinition">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="HouseNumber" type="xs:int" />
      <xs:element minOccurs="0" maxOccurs="1" name="StreetName" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Please let me know what is going on here.

Funiculate answered 1/11, 2015 at 7:17 Comment(0)
P
21

You have to tell xsd.exe all the schema referenced

xsd.exe /c "C:\Temp\A2.xsd" "C:\Temp\A21.xsd" "C:\Temp\A22.xsd"
Protolanguage answered 1/11, 2015 at 10:5 Comment(1)
Wow, so simple. Been driving me nuts.ThanksSchizomycete
A
2

If you have a lot of xsd, you should use a configuration file. For example GenerateClassFromXSD.xml :

<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='CS' namespace='Namespace.subnamespace'>
    <schema>A2.xsd</schema>
    <schema>A21.xsd</schema>
    <schema>.\A22.xsd</schema>
</generateClasses>
</xsd>

xsd /p:GenerateClassFromXSD.xml

See the final tips here : Tool that can combine many XSD files into one?

Accede answered 4/6, 2021 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.