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

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

ds.readxmlSchema(s);

两个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>

解决方法:

你应该看看Deriving DataSet Relational Structure from 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.

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.

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

>订单
>项目

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

编辑

请进一步查看Generating DataSet Relations for 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.

<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的列,但您无法阻止此功能

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念