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

Gar*_*ett 4 c# xml xsd dataset

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

ds.ReadXmlSchema(s);
Run Code Online (Sandbox Code Playgroud)

两个DataTable都有一个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>
Run Code Online (Sandbox Code Playgroud)

Pil*_*anz 5

您应该看一下从XML Schema(XSD)派生数据集关系结构.这篇文章指出了这一点

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

...

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

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

  1. 订购
  2. 项目

由于项目复杂类型嵌套秩序复杂类型中的一个关系将被这两个表之间产生了.为了能够创建此关系,将包括新列Order_id.

编辑

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

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>
Run Code Online (Sandbox Code Playgroud)

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