[ Team LiB ] |
The XmlDataDocument ObjectIn Chapter 9 we discussed the XmlDocument object and how it is used to access the hierarchical data of the nodes of an XML document loaded into memory. We have also discussed throughout this book how to retrieve and access relational data from a (traditional) SQL database. On the one hand, what if your data comes from an XML source, but you want (or only know how) to navigate and manipulate it by using rows and relational techniques? On the other hand, what if your data comes from an SQL database and you want to navigate and manipulate it by using nodes and XML techniques? The answer to these questions is the XmlDataDocument class. It is derived from the XmlDocument class but adds an important dimension. While maintaining a single internal copy of data, it provides both XML node access, as the XmlDocument class, and relational access via a DataSet property. The XmlDataDocument automatically synchronizes the two views (or access methods), so that any change via one technique or technology is immediately accessible via the other. This approach allows the mixing and matching of data sources and access techniques. Continuing with the ADO-XML project, let's look at two scenarios.
The subroutine btnNavigateSQL_Click reads in data from a SQL Server database and then navigates a subset of the records via an XPATH query to iterate over the selected data. The key lines in this routine are
First, the filled DataSet is associated with a new XmlDataDocument. An XPathNavigator is created on this XmlDataDocument, allowing us to create an XPathNodeIterator. An XPATH query string is passed to the Select method, where the query is defined to return the author last name (au_lname) field, for all the author nodes where the state is "CA". The routine then iterates across all the nodes selected by the query, building a string that contains the author last names. This string is then displayed in a message box, as shown in Figure 10.3. Figure 10.3. Message box with XML data retrieved directly from a DataSetThe second subroutine, btnAddRows_Clicks, goes the other way. It first executes ReadXml to read XML data from the file publishers.xml into the dsPubs DataSet, as in Listing 10.1. This method automatically creates a publishers table in the DataSet. The subroutine then adds new data via relational techniques and objects such as the DataRow. A new DataRow is created with the schema of the publishers table, and the row columns are assigned values. The new row is added to the publishers table and the result is displayed in a DataGrid, as shown in Figure 10.4. Figure 10.4. DataGrid with data from XML file and added row |
[ Team LiB ] |
1 comment:
Can i has this in C#?
Vb = virtually brainless.
Post a Comment