Generating DataSet Relations from XML Schema (XSD)

In a DataSet, you form an association between two or more columns by creating a parent-child relation. There are three ways to represent a DataSet relation within an XML Schema definition language (XSD) schema:

  • Specify nested complex types.

  • Use the msdata:Relationship annotation.

  • Specify an xs:keyref without the msdata:ConstraintOnly annotation.

Nested Complex Types

Nested complex type definitions in a schema indicate the parent-child relationships of the elements. The following XML Schema fragment shows that OrderDetail is a child element of the Order element.

<xs:element name="Order">  
       <xs:element name="OrderDetail" />  

The XML Schema mapping process creates tables in the DataSet that correspond to the nested complex types in the schema. It also creates additional columns that are used as parent-child columns for the generated tables. Note that these parent-child columns specify relationships, which is not the same as specifying primary key/foreign key constraints.

msdata:Relationship Annotation

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.

<msdata:Relationship name="CustOrderRelationship"
msdata:childkey="" />  

The attributes of the msdata:Relationship annotation identify the elements involved in the parent-child relationship, as well as the parentkey and childkey elements and attributes involved in the relationship. The mapping process uses this information to generate tables in the DataSet and to create the primary key/foreign key relationship between these tables.

For example, the following schema fragment specifies Order and OrderDetail elements at the same level (not nested). The schema specifies an msdata:Relationship annotation, which specifies the parent-child relationship between these two elements. In this case, an explicit relationship must be specified using the msdata:Relationship annotation.

 <xs:element name="MyDataSet" msdata:IsDataSet="true">  
    <xs:choice maxOccurs="unbounded">  
        <xs:element name="OrderDetail">  
       <xs:element name="Order">  
       <msdata:Relationship name="OrdOrdDetailRelation"  

The mapping process uses the Relationship element to create a parent-child relationship between the OrderNumber column in the Order table and the OrderNo column in the OrderDetail table in the DataSet. The mapping process only specifies the relationship; it does not automatically specify any constraints on the values in these columns, as do the primary key/foreign key constraints in relational databases.

In This Section

Map Implicit Relations Between Nested Schema Elements
Describes the constraints and relations that are implicitly created in a DataSet when nested elements are encountered in XML Schema.

Map Relations Specified for Nested Elements
Describes how to explicitly set relations in a DataSet for nested elements in XML Schema.

Specify Relations Between Elements with No Nesting
Describes how to create relations in a DataSet between XML Schema elements that are not nested.

Deriving DataSet Relational Structure from XML Schema (XSD)
Describes the relational structure, or schema, of a DataSet that is created from XML Schema definition language (XSD) schema.

Mapping XML Schema (XSD) Constraints to DataSet Constraints
Describes the XML Schema elements used to create unique and foreign key constraints in a DataSet.

See also