XPathBinder.Select Method (Object, String)


Uses an XPath data-binding expression at run time to return a list of nodes.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public static IEnumerable Select(
	object container,
	string xPath


Type: System.Object

The IXPathNavigable object reference that the expression is evaluated against. This must be a valid object identifier in the page's specified language.

Type: System.String

The XPath query that retrieves a list of nodes.

Return Value

Type: System.Collections.IEnumerable

An IEnumerable list of nodes.

Exception Condition

The container or xpath parameter is null.


The object specified by container is not an IXPathNavigable.


The current node of the XPathNodeIterator does not have an associated XML node.

You can use the Select(Object, String) method declaratively if you want to simplify the retrieval of a set of nodes using an XPath query. To do so, you must place the <%# and %> tags, which are also used in standard ASP.NET data binding, around the XPath query.

For any of the list ASP.NET server controls, such as DataList, DataGrid, or Repeater, the container parameter should be Container.DataItem.

The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data. This example has two parts:

  • A Web Forms page that displays XML data.

  • An XML file that contains the data.

The first part of the example shows a Web Forms page that displays XML data accessed through an XmlDataSource control. A Repeater control uses the simplified Eval(Object, String) method syntax to bind to data items within the XML document that the XmlDataSource represents. It uses the Select(Object, String) method to retrieve an IEnumerable list and assign it as a late-bound DataSource property for the Repeater control.

<%@ 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">
    <form id="form1" runat="server">
        DataFile="order.xml" />

      <asp:Repeater ID="Repeater1"
                <td>Ship To</td>
            <h3>Order Summary</h3>
            <asp:Repeater ID="Repeater2"
                     <b><%#XPath("@dept")%></b> -
                         <%#XPath(".")%><br />
            <hr />


The second example provides the XML file, Order.xml, that is used as the source of the data displayed in the Web Forms page defined above.

<?xml version="1.0" encoding="iso-8859-1"?>
      <customer id="12345" />
      <transaction id="12345" />
        <address1>1234 Tenth Avenue</address1>
        <item dept="tools">screwdriver</item>
        <item dept="tools">hammer</item>
        <item dept="plumbing">fixture</item>

.NET Framework
Available since 2.0
Return to top