Export (0) Print
Expand All

XmlDataSource.XPath Property

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.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual string XPath { get; set; }
/** @property */
public String get_XPath ()

/** @property */
public void set_XPath (String value)

public function get XPath () : String

public function set XPath (value : String)

Not applicable.

Property Value

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. The default value is String.Empty.

Exception typeCondition

InvalidOperationException

The document is loading.

For more information on XPath data-binding expressions, see Binding a Tabular Control to the XmlDataSource Control.

TopicLocation
Walkthrough: Creating a Web Page to Display XML DataBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying an XML Document in a Web Forms Page Using TransformationsBuilding ASP .NET Web Applications in Visual Studio

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. 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="VJ#" %>
<!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>

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>

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft