Share via


Mapping Relations Specified for Nested Elements 

A schema can include an msdata:Relationship annotation to explicitly specify the mapping between any two elements in the schema. The two elements specified in msdata:Relationship can be nested in the schema, but do not have to be. The mapping process uses msdata:Relationship in the schema to generate the primary key/foreign key relationship between the two columns.

The following example shows an XML Schema in which the OrderDetail element is a child element of Order. The msdata:Relationship identifies this parent-child relationship and specifies that the OrderNumber column of the resulting Order table is related to the OrderNo column of the resulting OrderDetail table.

<xs:schema id="MyDataSet"  
            xmlns:xs="https://www.w3.org/2001/XMLSchema" 
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="MyDataSet" msdata:IsDataSet="true">
 <xs:complexType>
  <xs:choice maxOccurs="unbounded">
   <xs:element name="Order">
    <xs:complexType>
     <xs:sequence>
       <xs:element name="OrderNumber" type="xs:string" />
       <xs:element name="EmpNumber" type="xs:string" />
       <xs:element name="OrderDetail">
          <xs:annotation>
           <xs:appinfo>
            <msdata:Relationship name="OrdODRelation" 
                                msdata:parent="Order" 
                                msdata:child="OrderDetail" 
                                msdata:parentkey="OrderNumber" 
                                msdata:childkey="OrderNo"/>
           </xs:appinfo>
          </xs:annotation>
          <xs:complexType>
            <xs:sequence>
             <xs:element name="OrderNo" type="xs:string" />
             <xs:element name="ItemNo" type="xs:string" />
            </xs:sequence>
         </xs:complexType>
       </xs:element>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:choice>
 </xs:complexType>
</xs:element>
</xs:schema>

The XML Schema mapping process creates the following in the DataSet:

  • An Order and an OrderDetail table.

    Order(OrderNumber, EmpNumber)
    OrderDetail(OrderNo, ItemNo)
    
  • A relationship between the Order and OrderDetail tables. The Nested property for this relationship is set to True because the Order and OrderDetail elements are nested in the schema.

    ParentTable: Order
    ParentColumns: OrderNumber 
    ChildTable: OrderDetail
    ChildColumns: OrderNo 
    RelationName: OrdODRelation
    Nested: True
    

The mapping process does not create any constraints.

See Also

Concepts

Generating DataSet Relations from XML Schema (XSD)

Other Resources

Mapping XML Schema (XSD) Constraints to DataSet Constraints