通过


XPathBinder.Select 方法

定义

在运行时使用 XPath 数据绑定表达式返回节点列表。

重载

名称 说明
Select(Object, String)

在运行时使用 XPath 数据绑定表达式返回节点列表。

Select(Object, String, IXmlNamespaceResolver)

在运行时使用 XPath 数据绑定表达式返回节点列表,使用 IXmlNamespaceResolver 指定的对象解析 XPath 表达式中的命名空间前缀。

注解

如果要使用 XPath 查询简化一组节点的检索,则可以以声明方式使用重载 Select 方法。 为此,必须将%# 和 %> 标记放在 < XPath 查询周围,这些标记也用于标准 ASP.NET 数据绑定中。

Select(Object, String)

在运行时使用 XPath 数据绑定表达式返回节点列表。

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

参数

container
Object

IXPathNavigable计算表达式的对象引用。 这必须是页面指定语言中的有效对象标识符。

xPath
String

检索节点列表的 XPath 查询。

返回

IEnumerable节点列表。

例外

containerxpath参数为 null.

指定的 container 对象不是 IXPathNavigable.

当前节点 XPathNodeIterator 没有关联的 XML 节点。

示例

下面的代码示例演示如何将 XmlDataSource 控件与模板化 Repeater 控件一起使用以显示 XML 数据。 此示例有两个部分:

  • 显示 XML 数据的 Web 窗体页。

  • 包含数据的 XML 文件。

该示例的第一部分显示一个 Web 窗体页,该页显示通过控件访问的 XmlDataSource XML 数据。 Repeater控件使用简化Eval(Object, String)的方法语法绑定到表示的 XmlDataSource XML 文档中的数据项。 它使用 Select(Object, String) 该方法检索 IEnumerable 列表并将其指定为控件的后期绑定 DataSource 属性 Repeater

<%@ 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>

第二个示例提供 XML 文件(Order.xml),该文件用作上面定义的 Web 窗体页中显示的数据的源。

<?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>

注解

如果要使用 XPath 查询简化一组节点的检索,则可以以声明方式使用 Select(Object, String) 该方法。 为此,必须将%# 和 %> 标记放在 < XPath 查询周围,这些标记也用于标准 ASP.NET 数据绑定中。

对于任何列表 ASP.NET 服务器控件(例如DataListDataGridRepeatercontainer)参数应为 Container.DataItem

适用于

Select(Object, String, IXmlNamespaceResolver)

在运行时使用 XPath 数据绑定表达式返回节点列表,使用 IXmlNamespaceResolver 指定的对象解析 XPath 表达式中的命名空间前缀。

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

参数

container
Object

IXPathNavigable计算表达式的对象引用。 这必须是页面指定语言中的有效对象标识符。

xPath
String

检索节点列表的 XPath 查询。

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver用于解析 XPath 表达式中的命名空间前缀的对象。

返回

IEnumerable节点列表。

注解

如果要使用 XPath 查询简化一组节点的检索,则可以以声明方式使用 Select 该方法。 为此,必须将%# 和 %> 标记放在 < XPath 查询和对象周围,这些标记也用于标准 ASP.NET 数据绑定中,以及用于IXmlNamespaceResolver解析命名空间引用的对象。

对于任何列表 ASP.NET 服务器控件(例如DataListDataGridRepeatercontainer)参数应为 Container.DataItem

适用于