Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Gen Classes That Reference a Common Type

I am using XSD's to define my DTO types in C#. I am using XSD.exe to gen the classes from the XSD's.

I have a Common.xsd that defines an Address type and I want to use this in more than one class:

  <xs:complexType name="Address">
    <xs:sequence>
      <xs:element name="Street1" type="xs:string"/>
      <xs:element name="Street2" type="xs:string"/>
      <xs:element name="City" type="xs:string"/>
      <xs:element name="State" type="xs:string"/>
      <xs:element name="Zip" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Address" type="mhm:Address"/>

I am referencing this in a company XSD:

  <xs:include schemaLocation=".\Common.xsd"/>
  <xs:complexType name="Company">
    <xs:sequence>
      <xs:element name="AdmCode" type="xs:string"/>
      <xs:element name="CompanyCode" type="xs:string"/>
      <xs:element name="Name" type="xs:string"/>
      <xs:element ref="mhm:Address"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Company" type="mhm:Company"/>

And an employee XSD:

  <xs:include schemaLocation=".\Common.xsd"/>
  <xs:complexType name="Employee">
    <xs:sequence>
      <xs:element name="EmployeeNumber" type="xs:int"/>
      <xs:element name="FirstName" type="xs:string"/>
      <xs:element name="LastName" type="xs:string"/>
      <xs:element name="Address" type="mhm:Address"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="Employee" type="mhm:Employee"/>

I gen the classes using this command line:

xsd .\XSD\Common.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Employee.xsd /c /o:. /n:"DomainModel"
xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"

When I go to compile the project, I find that the Address type has been generated in both the Company.cs class file and the Employee.cs class file.

How can I get the Address type generated just once in the Common.cs class file and the Employee and Company types use this single Address type?

like image 822
RobertMS Avatar asked Nov 30 '11 15:11

RobertMS


People also ask

What .NET class transforms elements defined in XML schema definitions to common language runtime compatible data types?

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.

What is Xs in XML schema?

1.1 The Schema Namespace ( xs ) The XML representation of schema components uses a vocabulary identified by the namespace name http://www.w3.org/2001/XMLSchema . For brevity, the text and examples in this specification use the prefix xs: to stand for this namespace; in practice, any prefix can be used.

What is XSD C#?

XSD is a schema document that is used to validate a XML file. It validates the following things: Data type of tags (value of tags) Sequence of tags.

What is XSD path?

The XML Schema Definition tool (Xsd.exe) usually can be found in the following path: C:\Program Files (x86)\Microsoft SDKs\Windows\{version}\bin\NETFX {version} Tools\


1 Answers

You can use XSD.exe with multiple file arguments:

xsd .\XSD\Common.xsd .\XSD\Employee.xsd .\XSD\Company.xsd /c /o:. /n:"DomainModel"
like image 184
Matt Brunell Avatar answered Oct 03 '22 03:10

Matt Brunell