XPathBinder Class

Definition

为应用程序快速开发 (RAD) 设计器提供支持,以便分析使用 XPath 表达式的数据绑定表达式。Provides support for rapid application development (RAD) designers to parse data-binding expressions that use XPath expressions. 无法继承此类。This class cannot be inherited.

public ref class XPathBinder sealed
public sealed class XPathBinder
type XPathBinder = class
Public NotInheritable Class XPathBinder
Inheritance
XPathBinder

Examples

下面的代码示例演示如何将 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 窗体页。A Web Forms page that displays XML data.

  • 包含数据的 XML 文件。An XML file that contains the data.

该示例的第一部分显示一个 Web 窗体页,该页显示通过 XmlDataSource 控件访问的 XML 数据。The first part of the example shows a Web Forms page that displays XML data accessed through an XmlDataSource control. Repeater 控件使用简化 Eval(Object, String) 方法语法来绑定到 XmlDataSource 所表示的 XML 文档中的数据项。A Repeater control uses simplified Eval(Object, String) method syntax to bind to data items within the XML document that the XmlDataSource represents. 它使用 Select(Object, String) 方法检索 IEnumerable 列表,并将其分配为 Repeater 控件的后期绑定 DataSource 属性。It 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 文件的 XML 文件,该文件用作上面定义的 Web 窗体页中显示的数据源。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>  

Remarks

您可以使用此类的重载 Eval 方法绑定到针对实现 IXPathNavigable 接口(如 XmlNode)的对象执行的 XPath 表达式的结果。You can use the overloaded Eval method of this class to bind to the result of an XPath expression executed against an object that implements the IXPathNavigable interface, such as an XmlNode. 您可以使用 Select 方法检索针对 IXPathNavigable 对象执行的 XPath 表达式的结果,作为节点 IEnumerable 列表。You can use the Select method to retrieve the results of an XPath expression executed against an IXPathNavigable object as an IEnumerable list of nodes. 可以直接枚举此节点列表,或将其分配给列表控件的 DataSource 属性,如 RepeaterDataListThis list of nodes can be enumerated directly or assigned to the DataSource property of a list control, such as a Repeater or DataList.

以声明方式使用 XPathBinder 方法时,可以使用数据绑定语法的简化版本。You can use a simplified version of data-binding syntax when using the XPathBinder methods declaratively. 您可以使用 XPathxpath),而不是调用 XPathBinder.EvalContainer.DataItem``xpath)。Instead of calling XPathBinder.Eval(Container.DataItem, xpath) you can use XPath(xpath). 同样,您可以使用 XPathSelectxpath)来检索 IEnumerable 节点集,而不是调用 XPathBinder.SelectContainer.DataItemxpath)。Similarly, instead of calling XPathBinder.Select(Container.DataItem,xpath), you can use XPathSelect(xpath) to retrieve an IEnumerable set of nodes. 使用此简化的语法时,EvalSelect 方法将采用默认的 Container.DataItem 上下文对象。When using this simplified syntax, the Eval and Select methods assume a default Container.DataItem context object.

有关 ASP.NET 服务器控件的数据绑定的详细信息,请参阅数据绑定表达式语法For more information about data binding to ASP.NET server controls, see Data-Binding Expression Syntax.

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Eval(Object, String)

在运行时计算 XPath 数据绑定表达式。Evaluates XPath data-binding expressions at run time.

Eval(Object, String, IXmlNamespaceResolver)

使用指定用来解析 XPath 表达式中的命名空间前缀的 IXmlNamespaceResolver 对象,在运行时评估 XPath 数据绑定表达式,并将结果格式化为文本以便显示在请求浏览器中。Evaluates XPath data-binding expressions at run time and formats the result as text to be displayed in the requesting browser, using the IXmlNamespaceResolver object specified to resolve namespace prefixes in the XPath expression.

Eval(Object, String, String)

在运行时计算 XPath 数据绑定表达式,并将结果格式化为要在请求浏览器中显示的文本。Evaluates XPath data-binding expressions at run time and formats the result as text to be displayed in the requesting browser.

Eval(Object, String, String, IXmlNamespaceResolver)

使用指定用来解析 XPath 表达式中的命名空间前缀的 IXmlNamespaceResolver 对象,在运行时评估 XPath 数据绑定表达式,并将结果格式化为文本以便显示在请求浏览器中。Evaluates XPath data-binding expressions at run time and formats the result as text to be displayed in the requesting browser, using the IXmlNamespaceResolver object specified to resolve namespace prefixes in the XPath expression.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Select(Object, String)

在运行时使用 XPath 数据绑定表达式来返回节点列表。Uses an XPath data-binding expression at run time to return a list of nodes.

Select(Object, String, IXmlNamespaceResolver)

通过使用指定用来解析 XPath 表达式中的命名空间前缀的 IXmlNamespaceResolver,可以在运行时使用 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.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Applies to