XPathBinder.Select Method

Definition

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

Overloads

Select(Object, String)

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

Select(Object, String, IXmlNamespaceResolver)

Uses an XPath data-binding expression at run time to return a list of nodes, using the IXmlNamespaceResolver object specified to resolve namespace prefixes in the XPath expression.

Remarks

You can use the overloaded Select 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.

Select(Object, String)

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

public:
 static System::Collections::IEnumerable ^ Select(System::Object ^ container, System::String ^ xPath);
public static System.Collections.IEnumerable Select (object container, string xPath);
static member Select : obj * string -> System.Collections.IEnumerable
Public Shared Function Select (container As Object, xPath As String) As IEnumerable

Parameters

container
Object

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

xPath
String

The XPath query that retrieves a list of nodes.

Returns

An IEnumerable list of nodes.

Exceptions

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.

Examples

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">
    <title>Order</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:XmlDataSource
        runat="server"
        id="XmlDataSource1"
        XPath="orders/order"
        DataFile="order.xml" />

      <asp:Repeater ID="Repeater1"
        runat="server"
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
            <h2>Order</h2>
            <table>
              <tr>
                <td>Customer</td>
                <td><%#XPath("customer/@id")%></td>
                <td><%#XPath("customername/firstn")%></td>
                <td><%#XPath("customername/lastn")%></td>
              </tr>
              <tr>
                <td>Ship To</td>
                <td><%#XPath("shipaddress/address1")%></font></td>
                <td><%#XPath("shipaddress/city")%></td>
                <td><%#XPath("shipaddress/state")%>,
                    <%#XPath("shipaddress/zip")%></td>
              </tr>
            </table>
            <h3>Order Summary</h3>
            <asp:Repeater ID="Repeater2"
                 DataSource='<%#XPathSelect("summary/item")%>'
                 runat="server">
                <ItemTemplate>
                     <b><%#XPath("@dept")%></b> -
                         <%#XPath(".")%><br />
                </ItemTemplate>
            </asp:Repeater>
            <hr />
        </ItemTemplate>
    </asp:Repeater>

  </form>
  </body>
</html>
<%@ 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>Order</title>
</head>
<body> 
    <form id="form1" runat="server">
      <asp:XmlDataSource
        runat="server"
        id="XmlDataSource1"
        XPath="orders/order"
        DataFile="order.xml" />

      <asp:Repeater ID="Repeater1"
        runat="server"
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
            <h2>Order</h2>
            <table>
              <tr>
                <td>Customer</td>
                <td><%#XPath("customer/@id")%></td>
                <td><%#XPath("customername/firstn")%></td>
                <td><%#XPath("customername/lastn")%></td>
              </tr>
              <tr>
                <td>Ship To</td>
                <td><%#XPath("shipaddress/address1")%></font></td>
                <td><%#XPath("shipaddress/city")%></td>
                <td><%#XPath("shipaddress/state")%>,
                    <%#XPath("shipaddress/zip")%></td>
              </tr>
            </table>
            <h3>Order Summary</h3>
            <asp:Repeater ID="Repeater2"
                 DataSource='<%#XPathSelect("summary/item")%>'
                 runat="server">
                <ItemTemplate>
                     <b><%#XPath("@dept")%></b> -
                         <%#XPath(".")%><br />
                </ItemTemplate>
            </asp:Repeater>
            <hr />
        </ItemTemplate>
    </asp:Repeater>

  </form>
  </body>
</html>

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"?>  
  <orders>  
    <order>  
      <customer id="12345" />  
      <customername>  
        <firstn>John</firstn>  
        <lastn>Doe</lastn>  
      </customername>  
      <transaction id="12345" />  
      <shipaddress>  
        <address1>1234 Tenth Avenue</address1>  
        <city>Bellevue</city>  
        <state>Washington</state>  
        <zip>98001</zip>  
      </shipaddress>  
      <summary>  
        <item dept="tools">screwdriver</item>  
        <item dept="tools">hammer</item>  
        <item dept="plumbing">fixture</item>  
      </summary>  
    </order>  
  </orders>  

Remarks

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.

Applies to

Select(Object, String, IXmlNamespaceResolver)

Uses an XPath data-binding expression at run time to return a list of nodes, using the IXmlNamespaceResolver object specified to resolve namespace prefixes in the XPath expression.

public:
 static System::Collections::IEnumerable ^ Select(System::Object ^ container, System::String ^ xPath, System::Xml::IXmlNamespaceResolver ^ resolver);
public static System.Collections.IEnumerable Select (object container, string xPath, System.Xml.IXmlNamespaceResolver resolver);
static member Select : obj * string * System.Xml.IXmlNamespaceResolver -> System.Collections.IEnumerable
Public Shared Function Select (container As Object, xPath As String, resolver As IXmlNamespaceResolver) As IEnumerable

Parameters

container
Object

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

xPath
String

The XPath query that retrieves a list of nodes.

resolver
IXmlNamespaceResolver

The IXmlNamespaceResolver object used to resolve namespace prefixes in the XPath expression.

Returns

An IEnumerable list of nodes.

Remarks

You can use the Select 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 and an IXmlNamespaceResolver object to resolve the namespace reference.

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

Applies to