... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2008-06-11

Visual Studio XSD Tool vs XML Schema

The Visual Studio XSD tool cannot handle an XML Schema that uses a repeating element with the same name at two distinct places.  For example, it cannot handle this schema (in XQuery notation for brevity):

root {
  elementA {
    problemElement {
      name
    }
  },
  elementB {
    problemElement {
      name
    }
  }
}

The element named problemElement causes the problem.  If you attempt to generate C# code for this schema using the XSD tool, it all works.  But if you try to generate a dataset, you an error like:

Error: There was an error processing 'myproblematic.xsd'.
  - The same table (ProblemElement) cannot be the child table in two nested relations.

The solution is to rename one of the problematic elements to reflect the context (sigh).

I have a minimal xsd that demonstrates the issue:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    targetNamespace="urn:tns"
    xmlns:tns="urn:tns"
    >

  <xs:element name="Root" type="tns:Root"/>

  <xs:complexType name="Root">
    <xs:sequence>
      <xs:element name="ElementA">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ProblemElement">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name" type="xs:string"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="ElementB">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ProblemElement">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name" type="xs:string"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

And here is the command to process it:

C:\VS.NET2003\Common7\Tools\vsvars32.bat
xsd /d dotnet-xsd-bug.xsd

Blog Archive