asp.net.ph

Skip Navigation Links

Binding a Tabular Control to the XmlDataSource Control

Controls You Can Use on Web Forms   ASP.NET Data Source Controls   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 Introduction to the XmlDataSource Control.

<html>
   <body>
      <form 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 valign=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>


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note