Compartir vía


XmlDataSource.XPath Propiedad

Definición

Especifica una expresión XPath que se aplicará a los datos XML contenidos por la Data propiedad o por el archivo XML indicado por la DataFile propiedad .

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

Valor de propiedad

Cadena que representa una expresión XPath que se puede usar para filtrar los datos contenidos por la Data propiedad o por el archivo XML indicado por la DataFile propiedad . El valor predeterminado es Empty.

Excepciones

El documento se está cargando.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar un XmlDataSource control con plantilla Repeater para mostrar datos XML filtrados mediante una expresión XPath. En este ejemplo, se usa la sintaxis XPath para filtrar los datos XML cuando se establece la XPath propiedad del control de origen de datos y también enlaza elementos de la Repeater plantilla de control a los datos XML.

<%@ 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>

El archivo XML del ejemplo de código tiene los siguientes datos:

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

Comentarios

Para obtener más información sobre las expresiones de enlace de datos XPath, vea Enlace de un control tabular al control XmlDataSource.

Se aplica a