nelsontruran
nelsontruran

Reputation: 554

Many XSDs sharing elements & types - Any way to generate classes? (c#)

I've been given around 160 XSDs and want to be able to generate c# classes, I'm not allowed to change the XSDs and they may add more schemas that follow the format that they gave to me.

Each schema declares the root element of the same name with many matching types e.g.:

Document 1:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root-XML" type="Root-XMLType1"/>
  <xs:complexType name="Root-XMLType1">
    <xs:sequence>
    <xs:element name="Header" type="HeaderStruct"/>
    <xs:element name="Delivery" type="DeliveryType1Struct"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="DeliveryType1Struct">
  <xs:complexContent>
  <xs:restriction base="DeliveryType1Abstract">
    <xs:all>
      <xs:element name="GMTOffset" type="GMTOffsetType"/>
      <xs:element name="UserName" type="UserNameType"/>
    </xs:all>
  </xs:restriction>
  </xs:complexContent>
</xs:complexType>
..... 9000 more lines of types .....
</xs:schema>

Document 2:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root-XML" type="Root-XMLType2"/>
  <xs:complexType name="Root-XMLType2">
    <xs:sequence>
    <xs:element name="Header" type="HeaderStruct"/>
    <xs:element name="Delivery" type="DeliveryType2Struct"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="DeliveryType2Struct">
  <xs:complexContent>
  <xs:restriction base="DeliveryType2Abstract">
    <xs:all>
      <xs:element name="GMTOffset" type="GMTOffsetType"/>
      <xs:element name="UserName" type="UserNameType"/>
    </xs:all>
  </xs:restriction>
  </xs:complexContent>
</xs:complexType>
..... 9000 more lines of types .....
</xs:schema>

Most of these schemas are the same except for a few added elements here and there.

I tried to use XSD.exe but there were 'this type was already declared' warnings and many missing elements; it seemed like it just picked the last XSD passed to it in the parameters file.

Upvotes: 0

Views: 51

Answers (1)

Sprotty
Sprotty

Reputation: 5973

You could use xsd.exe to generate a set of classes for each schema, and have them each one go into its own .net namespace.

If you have xsd.exe generate partial classes, then you could then add interfaces to the ones that have commonality to allow you to work with them polymorphically as apposed to crafting code for each schema.

// Generated by XSD.exe
namespace A
{
  partial class GeneratedRootXML 
  {
      public string Header {get;set;}
      ....
  }
}

// Generated by XSD.exe
namespace B
{
  partial class GeneratedRootXML 
  {
      public string Header {get;set;}
      ....
  }
}

// Your code
interface IMyRootXML
{
   public string Header {get;set;}
}

namespace A
{
  partial class GeneratedRootXML : IMyRootXML
  {
  }
}

namespace B
{
  partial class GeneratedRootXML : IMyRootXML
  {
  }
}

But how ever you do it its going to be a bit of a mess.

Upvotes: 1

Related Questions