Nesting DataRelations (ADO.NET)

In a relational representation of data, individual tables contain rows that are related to one another using a column or set of columns. In the ADO.NET DataSet, the relationship between tables is implemented using a DataRelation. When you create a DataRelation, the parent-child relationships of the columns are managed only through the relation. The tables and columns are separate entities. In the hierarchical representation of data that XML provides, the parent-child relationships are represented by parent elements that contain nested child elements.

To facilitate the nesting of child objects when a DataSet is synchronized with an XmlDataDocument or written as XML data using WriteXml, the DataRelation exposes a Nested property. Setting the Nested property of a DataRelation to true causes the child rows of the relation to be nested within the parent column when written as XML data or synchronized with an XmlDataDocument. The Nested property of the DataRelation is false, by default.

Because the Nested property of the DataRelation object is not set to true for this DataSet, the child objects are not nested within the parent elements when this DataSet is represented as XML data. Transforming the XML representation of a DataSet that contains related DataSets with non-nested data relations can cause slow performance. We recommend that you nest the data relations. To do this, set the Nested property to true. Then write code in the XSLT style sheet that uses top-down hierarchical XPath query expressions to locate and transform the data.

The following code example shows the result from calling WriteXml on the DataSet.

Note that the Customers element and the Orders elements are shown as sibling elements. If you wanted the Orders elements to show up as children of their respective parent elements, the Nested property of the DataRelation would need to be set to true and you would add the following: