Compartir a través de


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="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:XmlDataSource
        id="PeopleDataSource"
        runat="server"
        XPath="/People/Person"
        DataFile="~/App_Data/people.xml" />

      <asp:DataList
        id="PeopleDataList"
        DataSourceID="PeopleDataSource"
        Runat="server">

        <ItemTemplate>
          <table cellpadding="4" cellspacing="4">
            <tr>
              <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>
              <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" />
              </td>
            </tr>
          </table>
        </ItemTemplate>
      </asp:DataList>
    </form>
  </body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:XmlDataSource
        id="PeopleDataSource"
        runat="server"
        XPath="/People/Person"
        DataFile="~/App_Data/people.xml" />

      <asp:DataList
        id="PeopleDataList"
        DataSourceID="PeopleDataSource"
        Runat="server">

        <ItemTemplate>
          <table cellpadding="4" cellspacing="4">
            <tr>
              <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>
              <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" />
              </td>
            </tr>
          </table>
        </ItemTemplate>
      </asp:DataList>
    </form>
  </body>
</html>

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.

<asp:XmlDataSource
  id="OrdersDataSource"
  runat="server"
  DataFile="~\App_Data\orders.xml" />

<asp:DataList
  id="OrdersDataList"
  DataSourceID="OrdersDataSource"
  Runat="server">

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

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

  </ItemTemplate>
</asp:DataList>

See Also

Other Resources

Data Source Web Server Controls