XML in .NET: DataSet and XmlDataDocument
The final article in our tour of the XML facilities in .NET ends with the data access framework of ADO.NET. ADO.NET has gone through a major overhaul to bring it in line with the XML- and Internet-centric nature of .NET. It eschews the server-side cursor model capability of ADO to concentrate on disconnected operations. The data vehicle in ADO.NET for storing, manipulating, and passing data between tiers is the DataSet. As an added benefit, the DataSet handles hierarchical data with aplomb and has the ability to parse and generate XML from its data.
A Disconnected Database
The object model of the DataSet mirrors the structure of a relational database. It begins with a collection of DataTable objects that are analogous to tables in a database. Each DataTable is structured by a collection of DataColumn objects that describes what the collection of DataRow objects will hold. Tables are linked in a parent child relationship using the DataRelation object. Look at Figure 1 to see how the pieces fit together. Interfacing with relational databases is not the prime feature of this article, so we will leave it to the reader to research that portion of ADO.NET in more detail.
Figure 1 Disconnected database.
We are more interested in the XML support and the translation that the DataSet does between the concepts of tables/columns/rows and the element/attribute nature of XML. It does this through a set of heuristics that parse the XML into a nested set of tables. XML elements are mapped to DataTables with DataColumn objects representing the attributes on the elements or child elements with simple textual content. For elements with child elements with complex content, the child is mapped to a DataTable like the parent, and a DataRelation is created between the two. To create such a DataRelation, an additional DataColumn is added to the DataTable of both the parent and the child to meet the needs of a foreign key relationship. The basic concept is shown in Figure 2.
Figure 2 Mapping between relational and XML data.