XPathBinder.Select 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在執行時使用 XPath 資料綁定表達式來回傳節點清單。
多載
| 名稱 | Description |
|---|---|
| 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 節點清單。
例外狀況
container或xpath參數為 null。
由 指定的 container 物件並非 IXPathNavigable。
目前的 XPathNodeIterator 節點沒有相關的 XML 節點。
範例
以下程式碼範例示範如何使用 XmlDataSource 帶有模板控制 Repeater 項的控制項來顯示 XML 資料。 這個例子有兩部分:
一個顯示 XML 資料的網頁表單頁面。
一個包含資料的 XML 檔案。
範例的第一部分展示了一個網頁表單頁面,顯示透過 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,作為上述網頁表單頁面中顯示資料的來源。
<?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 伺服器控制項,如 DataList、 DataGrid、 Repeater或 ,參數 container 應為 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 方法。 為此,您必須將 <%# 和 %> 標籤(這兩者也用於標準 ASP.NET 資料綁定)置於 XPath 查詢周圍,並設置 IXmlNamespaceResolver 物件以解析命名空間的參考。
對於列表中 ASP.NET 伺服器控制項,如 DataList、 DataGrid、 Repeater或 ,參數 container 應為 Container.DataItem。