4

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

ds.ReadXmlSchema(s);

两个数据表都有一个 Order_Id 列;其他列与 XSD 完全匹配。

有没有其他人见过这个?

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>
4

1 回答 1

5

您应该看看Deriving DataSet Relational Structure from XML Schema (XSD)。这篇文章指出

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

...

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

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

  1. 命令
  2. 物品

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

编辑

进一步了解为 XSD 生成数据集关系。在本文中,您会发现:

msdata :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>

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

于 2012-09-24T21:02:22.310 回答