FIX: Cannot derive and extend a column in XSD with DataSet (316820)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q316820
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data

SYMPTOMS

When you try to read an Extensible Markup Language (XML) schema into a DataSet, the following duplicate name exception may be thrown when the XML Schema Definition language (XSD) derives and extends a column definition:
An unhandled exception of type 'System.Data.DuplicateNameException' occurred in system.data.dll

Additional information: A column named 'column1' already belongs to this DataTable.
If you handle this exception within a try/catch block, you receive the following error message:
System.Data.DuplicateNameException: A column named 'column1' already belongs to this DataTable.

CAUSE

The DataSet is treating the column derivation and extension as if it were the definition of an entirely new column, with the same name as an existing column.

RESOLUTION

To avoid this exception, define a new element type with the attributes that you want, instead of deriving from and extending an existing element type.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. This bug was corrected in Microsoft ADO.NET (included with the .NET Framework 1.1), and Microsoft Visual Studio .NET 2003, Professional Edition.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a new XSD file named Test.xsd. Paste the following code into it:
    <?xml version="1.0" standalone="yes"?>
    <xsd:schema id="MyDataSet" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    
     <xsd:complexType name="columns" abstract="true"> 
       <xsd:sequence>
          <xsd:element name="column1" type="xsd:string"/>
       </xsd:sequence>
     </xsd:complexType>
    
     <xsd:element name="table1"> 
      <xsd:complexType> 
       <xsd:complexContent>
        <xsd:extension base="columns">
          <xsd:sequence>
           <xsd:element name="column1" msdata:ReadOnly="True" type="xsd:string"/>
          </xsd:sequence>
        </xsd:extension>
       </xsd:complexContent>
      </xsd:complexType>
     </xsd:element>
    
    </xsd:schema>
    						
  2. Create a new Visual Basic .NET console application.
  3. Paste the following code into the sub Main of Module1:
    Dim ds As New DataSet()
    ds.ReadXmlSchema("C:\Test.xsd") 'Exception thrown here.
    Console.Write("Schema Loaded Successfuly")
    Console.Read()
    					
  4. Change the string that is being passed in the call to ReadXML to reflect the path to the XML file that you created.
  5. Press F5 to run and compile the application. You receive the error message listed in the "Symptoms" section.
  6. In order to avoid this exception being thrown, replace code in the XSD file with the following code that defines a new element with the attributes that you want, instead of deriving from an existing element:
    <?xml version="1.0" standalone="yes" ?>
    <xsd:schema id="MyDataSet" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
     <xsd:element name="table1">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element name="column1" type="xsd:string" msdata:ReadOnly="True" />
       </xsd:sequence>
      </xsd:complexType>
     </xsd:element>
    </xsd:schema>
  7. Press F5 to run and compile the application. Output to the console confirms that the XSD schema was loaded successfully. Press ENTER to stop the running application, and then close the Console window.

REFERENCES

For more information about XML integration with ADO.NET, click the following article number to view the article in the Microsoft Knowledge Base:

313649 Roadmap for XML integration with ADO.NET

For more information about XML schemas in the .NET Framework, click the following article number to view the article in the Microsoft Knowledge Base:

313826 Roadmap for XML schemas in the .NET Framework


Modification Type:MinorLast Reviewed:3/10/2006
Keywords:kbvs2002sp1sweep kbfix kbbug kbinterop kbnofix kbSystemData KB316820 kbAudDeveloper