5

Currently I'm having a problem with generating class files from a xsd with repeating elements. I’m using the custom tool ‘MsDatasetGenerator’ in VS2005 SP1 witch create a typed dataset from the xsd for c#. I’m trying to parse the xml by this schema

    <?xml version="1.0" encoding=\"utf-8\"?>
<xs:schema id="XSDobject" targetNamespace="http://tempuri.org/XSDobject.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XSDobject.xsd" xmlns:mstns="http://tempuri.org/XSDobject.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="order">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="contact">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string" />
                            <xs:element name="phone" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="buyer">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="contact">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="name" type="xs:string" />
                                        <xs:element name="phone" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

But I get following error “The same table 'contact' cannot be the child table in two nested relations.

The XSD compiles correctly but it’s the typed dataset that can’t handle repeating tables. Now I have tested also the xsd.exe tool but it seems to generate the same code as the msdatasetgenerator. I also tried some third party code generator tools like XSD2Code, CodeXS, AltovaXmlSpy but also I can’t get it to work with nested elements.

Alternatively I could solve the problem with xslt transformation at the input and the output but it would cost me a lot of performance.

So now I’m asking if someone could help me with a good solution for VS2005, or know good xsd class generator that can handle this problem. It doesn’t have to be a typed dataset if it works as an array or a list it is also perfect as long it is easy to serializing and deserializing it.

Thanks in advance Freggel

freggel
  • 552
  • 2
  • 6
  • 13

4 Answers4

2

I had the same problem.. if performances are not an issue, you can use XSLT to rename the "child" tables that have the same name, (i.e. the resulting name is the concatenation of table name and its parent):

 ...
  <xsl:template match="*">
     <xsl:param name="parentElm">
       <xsl:value-of select="name(..)" />
     </xsl:param>
     <xsl:choose>
       <xsl:when test="local-name() = 'Contact'">
         <xsl:element name="{concat('Contact',$parentElm)}">
           <xsl:apply-templates select="@* | node()" />
         </xsl:element>
       </xsl:when> <xsl:otherwise>
         <xsl:element name="{local-name()}">
           <xsl:copy-of select="@*" />
           <xsl:apply-templates select="@* | node()" />
         </xsl:element>
       </xsl:otherwise>
     </xsl:choose>   </xsl:template> ...
socste2
  • 98
  • 1
  • 7
1

I would suggest a simple renaming of the schema items, plus the usage of grouping (shown below) or xsd includes (if you need this complex type for other schemas). This should solve to problem if you have no hard requirment on the names.

From experience, I don't think may tools will work with the repeated naming in your example.

Something like this may do the trick:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XSDobject" targetNamespace="http://tempuri.org/XSDobject.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XSDobject.xsd" xmlns:mstns="http://tempuri.org/XSDobject.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="Contact">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
      <xs:element name="phone" type="xs:string" />
    </xs:sequence>
  </xs:group>
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="OrderContact">
          <xs:complexType>
            <xs:sequence>
              <xs:group ref="Contact"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="buyer">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="BuyerContact">
                <xs:complexType>
                  <xs:sequence>
                    <xs:group ref="Contact"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
nick_alot
  • 2,982
  • 5
  • 30
  • 32
0

Maybe you can use the xsd:import / xsd:include to split the xsd into several files, then use xsd.exe to compile each. I think you can specify the namespace to generate the code to when working with xsd.exe.

I worked with generating classes from xsd:s a couple of years ago, and for some reason I decided on using xsdobjgen.exe instead of xsd.exe.

Good luck!

0

Look at my solution for this item.

The same table 'name' cannot be the child table in two nested relations

I suggest using 'ref' in your schema to refer to the 'duplicate' elements.

Kabwla-TwoLips
  • 101
  • 1
  • 6