为什么ReadXmlSchema会创建额外的“ID”列

时间:2023-02-12 08:46:56

Given an XSD file, code like the following produces an extra (and unwanted) column in both DataTables in the returned DataSet.

给定一个XSD文件,如下所示的代码会在返回的DataSet中的两个DataTable中生成一个额外(和不需要的)列。

ds.ReadXmlSchema(s);

Both DataTables have an Order_Id column; other columns perfectly match the XSD.

两个DataTable都有一个Order_Id列;其他列与XSD完美匹配。

Has anyone else seen this before?

有没有其他人见过这个?

XSD file below:

XSD文件如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType msdata:AutoIncrement="false">
            <xs:attribute name="itemId" type="xs:unsignedInt" />
            <xs:attribute name="stockCode" type="xs:string" />
            <xs:attribute name="stockCodeType" type="xs:string" />
            <xs:attribute name="Quantity" type="xs:unsignedLong" />
            <xs:attribute name="ProductIdX" type="xs:unsignedInt" />
            <xs:attribute name="legalEntity" type="xs:string" />
            <xs:attribute name="countryOfIssue" type="xs:string" />
            <xs:attribute name="branchSystem" type="xs:string" />
            <xs:attribute name="accountId" type="xs:string" />
            <xs:attribute name="settlementDate" type="xs:string" />
            <xs:attribute name="tradeDate" type="xs:string" />
            <xs:attribute name="partyCode" type="xs:string" />
            <xs:attribute name="userId" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="OrderId" type="xs:unsignedInt" />
      <xs:attribute name="StrategyId" type="xs:string" />
      <xs:attribute name="ActivityId" type="xs:string" />
    </xs:complexType>
  </xs:element>
</xs:schema>

1 个解决方案

#1


5  

You should have a look at Deriving DataSet Relational Structure from XML Schema (XSD). This article points out that

您应该看一下从XML Schema(XSD)中获取数据集关系结构。这篇文章指出了这一点

In general, for each complexType child element of a schema element, a table is generated in the DataSet. The table structure is determined by the definition of the complex type.

通常,对于schema元素的每个complexType子元素,将在DataSet中生成一个表。表结构由复杂类型的定义决定。

...

However, a table is only created for a top-level complexType element when the complexType element is nested inside another complexType element, in which case the nested complexType element is mapped to a DataTable within the DataSet.

但是,仅当complexType元素嵌套在另一个complexType元素内时,才会为*complexType元素创建表,在这种情况下,嵌套的complexType元素将映射到DataSet中的DataTable。

So basically in this case ReadXML(...) will create two tables

所以基本上在这种情况下,ReadXML(...)将创建两个表

  1. Order
  2. Item

As Item complexType is nested within Order complexType a relation between those two tables will be generated too. To be able to create this relation a new column Order_id will be included.

由于item complexType嵌套在Order complexType中,因此也将生成这两个表之间的关系。为了能够创建此关系,将包含新列Order_id。

EDIT

Have a further look at Generating DataSet Relations for XSD. Within this article you will find this:

进一步了解为XSD生成数据集关系。在本文中,您将找到:

The msdata:Relationship annotation allows you to explicitly specify parent-child relationships between elements in the schema that are not nested. The following example shows the structure of the Relationship element.

msdata:Relationship批注允许您显式指定架构中未嵌套的元素之间的父子关系。以下示例显示了Relationship元素的结构。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Order">
     ... your definition goes here!
 </xs:element>
 <xs:annotation>
   <xs:appinfo>
     <msdata:Relationship name="OrderItemRelation"
      msdata:parent="Order"
      msdata:child="Item" 
      msdata:parentkey="OrderID"
      msdata:childkey="ANY_COLUMN_IN_NESTED_COMPLEX_TYPE"/>
   </xs:appinfo>
  </xs:annotation>
</xs:schema>

So you can modify what column will be used for referring inner to outer complexType but you can not prevent this functionality!

因此,您可以修改将用于引用内部到外部complexType的列,但您无法阻止此功能!

#1


5  

You should have a look at Deriving DataSet Relational Structure from XML Schema (XSD). This article points out that

您应该看一下从XML Schema(XSD)中获取数据集关系结构。这篇文章指出了这一点

In general, for each complexType child element of a schema element, a table is generated in the DataSet. The table structure is determined by the definition of the complex type.

通常,对于schema元素的每个complexType子元素,将在DataSet中生成一个表。表结构由复杂类型的定义决定。

...

However, a table is only created for a top-level complexType element when the complexType element is nested inside another complexType element, in which case the nested complexType element is mapped to a DataTable within the DataSet.

但是,仅当complexType元素嵌套在另一个complexType元素内时,才会为*complexType元素创建表,在这种情况下,嵌套的complexType元素将映射到DataSet中的DataTable。

So basically in this case ReadXML(...) will create two tables

所以基本上在这种情况下,ReadXML(...)将创建两个表

  1. Order
  2. Item

As Item complexType is nested within Order complexType a relation between those two tables will be generated too. To be able to create this relation a new column Order_id will be included.

由于item complexType嵌套在Order complexType中,因此也将生成这两个表之间的关系。为了能够创建此关系,将包含新列Order_id。

EDIT

Have a further look at Generating DataSet Relations for XSD. Within this article you will find this:

进一步了解为XSD生成数据集关系。在本文中,您将找到:

The msdata:Relationship annotation allows you to explicitly specify parent-child relationships between elements in the schema that are not nested. The following example shows the structure of the Relationship element.

msdata:Relationship批注允许您显式指定架构中未嵌套的元素之间的父子关系。以下示例显示了Relationship元素的结构。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Order">
     ... your definition goes here!
 </xs:element>
 <xs:annotation>
   <xs:appinfo>
     <msdata:Relationship name="OrderItemRelation"
      msdata:parent="Order"
      msdata:child="Item" 
      msdata:parentkey="OrderID"
      msdata:childkey="ANY_COLUMN_IN_NESTED_COMPLEX_TYPE"/>
   </xs:appinfo>
  </xs:annotation>
</xs:schema>

So you can modify what column will be used for referring inner to outer complexType but you can not prevent this functionality!

因此,您可以修改将用于引用内部到外部complexType的列,但您无法阻止此功能!