XmlDataSource.XPath 屬性

定義

指定要套用至下列 XML 資料的 XPath 運算式:Data 屬性包含的 XML 資料,或 DataFile 屬性所表示之 XML 檔案包含的 XML 資料。Specifies an XPath expression to be applied to the XML data contained by the Data property or by the XML file indicated by the DataFile property.

public:
 virtual property System::String ^ XPath { System::String ^ get(); void set(System::String ^ value); };
public virtual string XPath { get; set; }
member this.XPath : string with get, set
Public Overridable Property XPath As String

屬性值

String

字串,表示可用來篩選下列資料的 XPath 運算式:Data 屬性包含的資料,或 DataFile 屬性所表示之 XML 檔案包含的資料。A string that represents an XPath expression that can be used to filter the data contained by the Data property or by the XML file indicated by the DataFile property. 預設值是 EmptyThe default value is Empty.

例外狀況

此文件正在載入。The document is loading.

範例

下列程式碼範例示範如何搭配樣板 XmlDataSource 化控制項使用控制項 Repeater ,以顯示已使用 XPath 運算式篩選的 XML 資料。The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data that has been filtered using an XPath expression. 在此範例中,XPath 語法是在設定資料來源控制項的屬性時用來篩選 XML 資料 XPath ,同時也會將控制項範本的元素系結 Repeater 至 xml 資料。In this example, XPath syntax is used to filter the XML data when the XPath property of the data source control is set, and also binds elements of the Repeater control template to the XML data.

<%@ 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>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:XmlDataSource
        id="XmlSource"
        DataFile="bookstore.xml"
        runat="server"
        XPath="bookstore/genre[@name='fiction']"/>

      <asp:Repeater
        DataSourceID="XmlSource"
        runat="server">
          <ItemTemplate>
              <h1><%# XPath ("book/title") %></h1>
              <b>Price:</b>
              <%# XPath ("book/price") %>
          </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>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:XmlDataSource
        id="XmlSource"
        DataFile="bookstore.xml"
        runat="server"
        XPath="bookstore/genre[@name='fiction']"/>

      <asp:Repeater
        DataSourceID="XmlSource"
        runat="server">
          <ItemTemplate>
              <h1><%# XPath ("book/title") %></h1>
              <b>Price:</b>
              <%# XPath ("book/price") %>
          </ItemTemplate>
      </asp:Repeater>
    </form>

  </body>
</html>

程式碼範例中的 XML 檔案包含下列資料:The XML file in the code example has the following data:

<bookstore>  
   <genre name="fiction">  
     <book ISBN="0000000000">  
       <title>Secrets of Silicon Valley</title>  
       <price>12.95</price>  
       <chapters>  
         <chapter num="1" name="Introduction" />             
         <chapter num="2" name="Body" />            
         <chapter num="3" name="Conclusion" />  
       </chapters>  
     </book>  
   </genre>  
   <genre name="novel">  
     <book genre="novel" ISBN="1111111111">  
       <title>Straight Talk About Computers</title>  
       <price>24.95</price>  
       <chapters>  
         <chapter num="1" name="Introduction" />   
         <chapter num="2" name="Body" />  
         <chapter num="3" name="Conclusion" />  
       </chapters>  
     </book>  
   </genre>  
</bookstore>  

備註

如需有關 XPath 資料系結運算式的詳細資訊,請參閱將 表格式控制項系結至 XmlDataSource 控制項For more information on XPath data-binding expressions, see Binding a Tabular Control to the XmlDataSource Control.

適用於