DOWNLOAD THE CODE:
Download the Code 39160.zip

XML views, which ship in SQL Server 2000 and the SQL Server 2000 Web releases (SQLXML), conveniently treat the data stored in your SQL Server database as if it were part of a virtual XML document. In "Defining XML Views," December 2002, InstantDoc ID 27106, I described how to create an annotated XML Schema Definition (XSD) schema (sometimes called a mapping schema) to build an XML view. These views are useful because they execute hierarchical XPath queries against your relational data. They accomplish this feat by combining the data in the mapping schema with an XPath query to produce a SQL query, which in turn generates the XML result.

Originally, XML views were the only method for querying your data in this way. But since the introduction of the Microsoft .NET Framework, not only can you execute hierarchical queries on relational data, but you can also manipulate the data—functionality that wasn't previously available. Let's look at how you can use ADO.NET's DataSet and XMLDataDocument classes to produce XML query results from relational data.

The DataSet
The DataSet is a client-side (or middle-tier) cache that stores data in a structure similar to a relational database; that is, it provides an in-memory relational representation of data retrieved to your local system. A DataSet lets you model your data by storing data in a set of tables that the DataTable class represents and by using key and foreign key columns to define relationships, called DataRelations, between the tables.

A DataSet also lets you navigate between tables by using those foreign key relationships when you access the data—an operation that's conceptually similar to a join in SQL Server. One of several ways you can populate a DataSet is to use a DataAdapter, which is an intermediary between a data source (such as a SQL Server database or an XML document) and a DataSet. A DataAdapter's Fill method executes a query in SQL Server, retrieves the data, then pours the data into the DataTables. (In a future column, I'll discuss how a DataAdapter also retrieves data directly from an XML document.) As you can see, a DataSet provides a familiar relational structure for in-memory, cached data. The DataSet also provides a relational object model similar to ADO for accessing the data, but that topic is beyond the scope of this article.

The XMLDataDocument
The XMLDataDocument class provides a virtual XML document whose contents are defined by data stored in a DataSet. Like SQLXML's XML views, XMLDataDocument requires a mapping between the relational storage of the DataSet and the hierarchical structure of the XML document that the XMLDataDocument provides. The following rules define that mapping:

  • Tables in the DataSet, which I call table elements, map to the element of the same name in the XML document.
  • Columns map to simple subelements of table elements.
  • A DataRelation established between two tables causes the table elements to be nested.

The XMLDataDocument class is a subclass of the XMLDocument class, which provides the Document Object Model (DOM) programming model for accessing and navigating the data in a virtual XML document. The DOM provides a tree-oriented programming model for XML documents that stores data in nodes of the tree. You can access data stored in the nodes by navigating the parent-child relationships that the DOM provides or by executing XPath queries to return a set of nodes that match the query. Additionally, because the DataSet and the XMLDataDocument share the same underlying in-memory storage, changes that you make by using the XMLDataDocument are reflected in the DataSet and vice versa. Now that you understand the basics of a DataSet and an XMLDataDocument, let's look at an example of how you can use them to produce XML query results.

   Prev. page   [1] 2     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.