给定一个 XSD 文件,像下面这样的代码会在返回的数据集中的两个数据表中产生一个额外的(不需要的)列。
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>
因此您可以修改将使用的列以将内部复杂类型引用到外部复杂类型,但您不能阻止此功能!
关于c# - 为什么 ReadXmlSchema 创建额外的 "ID"列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12572439/