This documentation is archived and is not being maintained.

Binding a Tabular Control to the XmlDataSource Control

The XmlDataSource control is used primarily to expose hierarchical XML data to bound controls such as the TreeView or Menu controls. You can also bind tabular data-bound controls such as a GridView or DataList control to the XmlDataSource control.

Binding to Fields in XML Data

When you bind a tabular data-bound control to the XmlDataSource control, the control renders only the first level of the XML hierarchy. The XmlDataSource control exposes the attributes of the first-level nodes as the equivalent of columns in a data table. As a result, you can specify an attribute name from the first-level node as the name of a DataField for a BoundField object. You can also specify an attribute name in an Eval data-binding expression in a control template.

Data Binding to the XML Hierarchy using the XPath Method

Tabular controls allow you to bind controls in a template to data using the Eval or Bind methods. If you are binding the tabular control to an XmlDataSource control, you can additionally use the XPath method, which enables you to specify an XPath query for read-only data binding.

The XPath data-binding method navigates the XML hierarchy and returns a value from a node or an attribute anywhere in the hierarchy. When using the XPath data-binding method, you pass an XPath query and an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.

The following code example shows a DataList control bound to an XmlDataSource control. The data-binding expression in the template uses the XPath method to return values from the XML data. For details about the People.xml file used for in the example, see XmlDataSource Control Overview.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
  <head runat="server">
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
        DataFile="~/App_Data/people.xml" />


          <table cellpadding="4" cellspacing="4">
              <td style="vertical-align:top; width:120">
                <asp:Label id="LastNameLabel" Text='<%# XPath("Name/LastName")%>' runat="server" />, 
                <asp:Label id="FirstNameLabel" Text='<%# XPath("Name/FirstName")%>' runat="server" />
              <td valign="top">
                 <asp:Label id="StreetLabel" Text='<%# XPath("Address/Street") %>' runat="server" /><br />
                 <asp:Label id="CityLabel" Text='<%# XPath("Address/City") %>' runat="server" />, 
                 <asp:Label id="RegionLabel" Text='<%# XPath("Address/Region") %>' runat="server" />
                 <asp:Label id="ZipCodeLabel" Text='<%# XPath("Address/ZipCode") %>' runat="server" />

Using XPathSelect to Return Selected Nodes

You can bind a nested tabular data control in an item template to a list of nodes selected from the data supplied by an XmlDataSource control. To do so, you can use the XPathSelect method. The XPathSelect method returns a list of nodes that match an XPath expression, which the tabular data control can work with as if they were a collection of data records. The following example shows how to use the XPathSelect method with a nested DataList control to display the Order_Details nodes from within the XML hierarchy.

  DataFile="~\App_Data\orders.xml" />


    Order ID: <asp:Label id="OrderIDLabel" runat="server" 
                Text='<%# XPath("OrderID") %> /><BR>

    <asp:DataList id="ProductsDataList" runat="server"
      DataSource='<%# XPathSelect("Order_Details") %>' >
        <br>Product ID:<%# XPath("ProductID")%>
        <br>Quantity: <%# XPath("Quantity") %>


See Also