Why ReadXmlSchema creates an additional column "ID"

Given an XSD file, code like the one below creates an extra (and unwanted) column in both data tables in the returned DataSet.

ds.ReadXmlSchema(s); 

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

Has anyone else seen this before?

XSD file below:

 <?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
source share
1 answer

You should take a look at Obtaining a Relational DataSet from an XML Schema (XSD) . This article indicates that

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

...

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

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

  • Order
  • Paragraph

As the Item complexType is located , nested in the Order complexType , a relationship will also be created between the two tables. To create this relationship, a new Order_id column will be included.

EDIT

Take a look at Creating DataSet Relationships for XSD . In this article you will find the following:

msdata: Relationship annotation allows you to explicitly specify relationships between parents and children between elements in a schema that are not nested. The following example shows the relationship structure of an 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> 

So, you can change which column will be used to refer to the internal to the external complexType, but you cannot prevent this functionality !

+5
source

Source: https://habr.com/ru/post/1436002/


All Articles