Share via


XPathBinder.Select 方法

定義

在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。

多載

Select(Object, String)

在執行階段使用 XPath 資料繫結運算式,以傳回節點清單。

Select(Object, String, IXmlNamespaceResolver)

透過使用 IXmlNamespaceResolver 物件 (指定用於解析 XPath 運算式中的命名空間前置詞),在執行階段使用 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 Form頁面。

  • 包含資料的 XML 檔案。

範例的第一個部分會顯示Web Form頁面,顯示透過 控制項存取的 XmlDataSource XML 資料。 Repeater控制項會使用簡化 Eval(Object, String) 的方法語法,系結至 XML 檔 XmlDataSource 內代表的資料項目。 它會使用 Select(Object, String) 方法來擷取 IEnumerable 清單,並將其指派為控制項的 Repeater 晚期繫結 DataSource 屬性。

<%@ 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頁面中所顯示之資料的來源。

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

透過使用 IXmlNamespaceResolver 物件 (指定用於解析 XPath 運算式中的命名空間前置詞),在執行階段使用 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 查詢和 IXmlNamespaceResolver 物件周圍,以在標準 ASP.NET 資料系結中使用,以解析命名空間參考。

對於任何清單 ASP.NET 伺服器控制項,例如 DataListDataGridRepeatercontainer 參數應該是 Container.DataItem

適用於