Share via


Performing an XPath Query on a DataSet 

The relationship between a synchronized DataSet and XmlDataDocument allows you to make use of XML services, such as the XML Path Language (XPath) query, that access the XmlDataDocument and can perform certain functionality more conveniently than accessing the DataSet directly. For example, rather than using the Select method of a DataTable to navigate relationships to other tables in a DataSet, you can perform an XPath query on an XmlDataDocument that is synchronized with the DataSet, to get a list of XML elements in the form of an XmlNodeList. The nodes in the XmlNodeList, cast as XmlElement nodes, can then be passed to the GetRowFromElement method of the XmlDataDocument, to return matching DataRow references to the rows of the table in the synchronized DataSet.

For example, the following code sample performs a "grandchild" XPath query. The DataSet is filled with three tables: Customers, Orders, and OrderDetails. In the sample, a parent-child relation is first created between the Customers and Orders tables, and between the Orders and OrderDetails tables. An XPath query is then performed to return an XmlNodeList of Customers nodes where a grandchild OrderDetails node has a ProductID node with the value of 43. In essence, the sample is using the XPath query to determine which customers have ordered the product that has the ProductID of 43.

' Assumes that connection is a valid SqlConnection.
connection.Open()
Dim dataSet As DataSet = New DataSet("CustomerOrders")
Dim customerAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM Customers", connection)
customerAdapter.Fill(dataSet, "Customers")

Dim orderAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM Orders", connection)
orderAdapter.Fill(dataSet, "Orders")

Dim detailAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM [Order Details]", connection)
detailAdapter.Fill(dataSet, "OrderDetails")

connection.Close()

dataSet.Relations.Add("CustOrders", _
dataSet.Tables("Customers").Columns("CustomerID"), _
dataSet.Tables("Orders").Columns("CustomerID")).Nested = true

dataSet.Relations.Add("OrderDetail", _
  dataSet.Tables("Orders").Columns("OrderID"), _
dataSet.Tables("OrderDetails").Columns("OrderID"), false).Nested = true

Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet) 
  
Dim nodeList As XmlNodeList = xmlDoc.DocumentElement.SelectNodes( _
  "descendant::Customers[*/OrderDetails/ProductID=43]")
  
Dim dataRow As DataRow
Dim xmlNode As XmlNode

For Each xmlNode In nodeList
  dataRow = xmlDoc.GetRowFromElement(CType(xmlNode, XmlElement))

  If Not dataRow Is Nothing then Console.WriteLine(xmlRow(0).ToString())
Next
// Assumes that connection is a valid SqlConnection.
connection.Open();

DataSet dataSet = new DataSet("CustomerOrders");

SqlDataAdapter customerAdapter = new SqlDataAdapter(
  "SELECT * FROM Customers", connection);
customerAdapter.Fill(dataSet, "Customers");

SqlDataAdapter orderAdapter = new SqlDataAdapter(
  "SELECT * FROM Orders", connection);
orderAdapter.Fill(dataSet, "Orders");

SqlDataAdapter detailAdapter = new SqlDataAdapter(
  "SELECT * FROM [Order Details]", connection);
detailAdapter.Fill(dataSet, "OrderDetails");

connection.Close();

dataSet.Relations.Add("CustOrders",
  dataSet.Tables["Customers"].Columns["CustomerID"],
 dataSet.Tables["Orders"].Columns["CustomerID"]).Nested = true;

dataSet.Relations.Add("OrderDetail",
  dataSet.Tables["Orders"].Columns["OrderID"],
  dataSet.Tables["OrderDetails"].Columns["OrderID"], 
  false).Nested = true;

XmlDataDocument xmlDoc = new XmlDataDocument(dataSet); 
  
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes(
  "descendant::Customers[*/OrderDetails/ProductID=43]");
  
DataRow dataRow;
foreach (XmlNode xmlNode in nodeList)
{
  dataRow = xmlDoc.GetRowFromElement((XmlElement)xmlNode);
  if (dataRow != null)
    Console.WriteLine(dataRow[0]);
}

See Also

Other Resources

Synchronizing a DataSet with an XmlDataDocument