XPathBinder.Select 方法

定義

在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。Uses an XPath data-binding expression at run time to return a list of nodes.

多載

Select(Object, String)

在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。Uses an XPath data-binding expression at run time to return a list of nodes.

Select(Object, String, IXmlNamespaceResolver)

透過使用 IXmlNamespaceResolver 物件 (指定用於解析 XPath 運算式中的命名空間前置詞),在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。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.

備註

Select如果您想要使用 XPath 查詢簡化一組節點的抓取,您可以用宣告的方式使用多載的方法。You can use the overloaded Select method declaratively if you want to simplify the retrieval of a set of nodes using an XPath query. 若要這樣做,您必須將 <%# and %> 標記(也用於標準 ASP.NET 資料系結)放在 XPath 查詢周圍。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)

在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。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

參數

container
Object

運算式所據以評估的 IXPathNavigable 物件參考。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

擷取節點清單的 XPath 查詢。The XPath query that retrieves a list of nodes.

傳回

IEnumerable

節點的 IEnumerable 清單。An IEnumerable list of nodes.

例外狀況

containerxpath 參數為 nullThe container or xpath parameter is null.

container 所指定的物件不是 IXPathNavigableThe object specified by container is not an IXPathNavigable.

目前的 XPathNodeIterator 節點沒有關聯的 XML 節點。The current node of the XPathNodeIterator does not have an associated XML node.

範例

下列程式碼範例示範如何使用具有樣板 XmlDataSource 化控制項的控制項 Repeater 來顯示 XML 資料。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:

  • 顯示 XML 資料的 Web Form 頁面。A Web Forms page that displays XML data.

  • 包含資料的 XML 檔。An XML file that contains the data.

範例的第一個部分會顯示 Web Form 頁面,該頁面會顯示透過控制項存取的 XML 資料 XmlDataSourceThe first part of the example shows a Web Forms page that displays XML data accessed through an XmlDataSource control. Repeater控制項使用簡化的 Eval(Object, String) 方法語法,系結至所代表之 XML 檔中的資料項目 XmlDataSourceA Repeater control uses the simplified Eval(Object, String) method syntax to bind to data items within the XML document that the XmlDataSource represents. 它會使用 Select(Object, String) 方法來取出 IEnumerable 清單,並將其指派為控制項的晚期繫結 DataSource 屬性 RepeaterIt 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>

第二個範例會提供 XML 檔案(Order.xml),此檔案是用來做為上面所定義 Web Form 頁面中顯示的資料來源。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>  

備註

Select(Object, String)如果您想要使用 XPath 查詢來簡化一組節點的抓取,您可以使用宣告方式來使用方法。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. 若要這樣做,您必須將 <%# and %> 標記(也用於標準 ASP.NET 資料系結)放在 XPath 查詢周圍。To do so, you must place the <%# and %> tags, which are also used in standard ASP.NET data binding, around the XPath query.

針對任何清單 ASP.NET 伺服器控制項(例如 DataListDataGridRepeater ), container 參數應該是 Container.DataItemFor any of the list ASP.NET server controls, such as DataList, DataGrid, or Repeater, the container parameter should be Container.DataItem.

適用於

Select(Object, String, IXmlNamespaceResolver)

透過使用 IXmlNamespaceResolver 物件 (指定用於解析 XPath 運算式中的命名空間前置詞),在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。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

參數

container
Object

運算式所據以評估的 IXPathNavigable 物件參考。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

擷取節點清單的 XPath 查詢。The XPath query that retrieves a list of nodes.

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver 物件,用來解析 XPath 運算式中的命名空間前置詞。The IXmlNamespaceResolver object used to resolve namespace prefixes in the XPath expression.

傳回

IEnumerable

節點的 IEnumerable 清單。An IEnumerable list of nodes.

備註

Select如果您想要使用 XPath 查詢來簡化一組節點的抓取,您可以使用宣告方式來使用方法。You can use the Select method declaratively if you want to simplify the retrieval of a set of nodes using an XPath query. 若要這樣做,您必須將 <%# and %> 標記(也用於標準 ASP.NET 資料系結)放在 XPath 查詢和物件的周圍, IXmlNamespaceResolver 以解析命名空間參考。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.

針對任何清單 ASP.NET 伺服器控制項(例如 DataListDataGridRepeater ), container 參數應該是 Container.DataItemFor any of the list ASP.NET server controls, such as DataList, DataGrid, or Repeater, the container parameter should be Container.DataItem.

適用於