Binding.XPath Property

Gets or sets an XPath query that returns the value on the XML binding source to use.

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

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

<object XPath="string" .../>

Property Value

The XPath query. The default value is a null reference (Nothing in Visual Basic).

When the binding source is XML data instead of a common language runtime (CLR) object, the XPath property is used instead of the Path property to indicate the path to the value on the binding source to use.

By setting the XPath property, you are creating an XPath query to select a node or a collection of nodes (System.Xml.XmlElement and System.Xml.XmlAttribute both derive from System.Xml.XmlNode). System.Xml.XmlNode is a common language runtime (CLR) object, which means you can bind to its properties using the Path property.

The XmlNode.SelectNodes method handles the XPath expressions from the XPath property. XPath functions are not supported. For XPath expression information, see the SelectNodes documentation and some examples provided in the Example section.

This example shows how to bind to XML data using an XmlDataProvider.

With an XmlDataProvider, the underlying data that can be accessed through data binding in your application can be any tree of XML nodes. In other words, an XmlDataProvider provides a convenient way to use any tree of XML nodes as a binding source.

In the following example, the data is embedded directly as an XML data island within the Resources section. An XML data island must be wrapped in <x:XData> tags and always have a single root node, which is Inventory in this example.


The root node of the XML data has an xmlns attribute that sets the XML namespace to an empty string. This is a requirement for applying XPath queries to a data island that is inline within the XAML page. In this inline case, the XAML, and thus the data island, inherits the System.Windows namespace. Because of this, you need to set the namespace blank to keep XPath queries from being qualified by the System.Windows namespace, which would misdirect the queries.


    <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
        <Inventory xmlns="">
            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
              <Title>XML in Action</Title>
              <Summary>XML Web Technology</Summary>
            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
              <Title>Programming Microsoft Windows With C#</Title>
              <Summary>C# Programming using the .NET Framework</Summary>
            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
              <Title>Inside C#</Title>
              <Summary>C# Language Programming</Summary>
            <Book ISBN="0-7356-1377-X" Stock="in" Number="5">
              <Title>Introducing Microsoft .NET</Title>
              <Summary>Overview of .NET Technology</Summary>
            <Book ISBN="0-7356-1448-2" Stock="out" Number="4">
              <Title>Microsoft C# Language Specifications</Title>
              <Summary>The C# language definition</Summary>
            <CD Stock="in" Number="3">
              <Title>Classical Collection</Title>
              <Summary>Classical Music</Summary>
            <CD Stock="out" Number="9">
              <Title>Jazz Collection</Title>
              <Summary>Jazz Music</Summary>

  <TextBlock FontSize="18" FontWeight="Bold" Margin="10"
    HorizontalAlignment="Center">XML Data Source Sample</TextBlock>
    Width="400" Height="300" Background="Honeydew">
      <Binding Source="{StaticResource InventoryData}"
               XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>

    <!--Alternatively, you can do the following. -->
    <!--<ListBox Width="400" Height="300" Background="Honeydew"
      ItemsSource="{Binding Source={StaticResource InventoryData},
      XPath=*[@Stock\=\'out\'] | *[@Number>\=8 or @Number\=3]}">-->

        <TextBlock FontSize="12" Foreground="Red">
            <Binding XPath="Title"/>

As shown in this example, to create the same binding declaration in attribute syntax you must escape the special characters properly. For more information, see &amp; and Related Character Entities.

The ListBox will show the following items when this example is run. These are the Titles of all of the elements under Books with either a Stock value of "out" or a Number value of 3 or greater than or equals to 8. Notice that no CD items are returned because the XPath value set on the XmlDataProvider indicates that only the Books elements should be exposed (essentially setting a filter).

In this example, the book titles are displayed because the XPath of the TextBlock binding in the DataTemplate is set to "Title". If you want to display the value of an attribute, such as the ISBN, you would set that XPath value to "@ISBN".

The XPath properties in WPF are handled by the XmlNode.SelectNodes method. You can modify the XPath queries to get different results. Here are some examples for the XPath query on the bound ListBox from the previous example:

  • XPath="Book[1]" will return the first book element ("XML in Action"). Note that XPath indexes are based on 1, not 0.

  • XPath="Book[@*]" will return all book elements with any attributes.

  • XPath="Book[last()-1]" will return the second to last book element ("Introducing Microsoft .NET").

  • XPath="*[position()>3]" will return all of the book elements except for the first 3.

When you run an XPath query, it returns an XmlNode or a list of XmlNodes. XmlNode is a common language runtime (CLR) object, which means you can use the Path property to bind to the common language runtime (CLR) properties. Consider the previous example again. If the rest of the example stays the same and you change the TextBlock binding to the following, you will see the names of the returned XmlNodes in the ListBox. In this case, the name of all the returned nodes is "Book".

<TextBlock FontSize="12" Foreground="Red">
    <Binding Path="Name"/>

For the complete code sample, see XML Data Source Sample.

In some applications, embedding the XML as a data island within the source of the XAML page can be inconvenient because the exact content of the data must be known at compile time. Therefore, obtaining the data from an external XML file is also supported, as in the following example:

<XmlDataProvider x:Key="BookData" Source="data\bookdata.xml" XPath="Books"/>

For the complete code sample, see XML Data Source with Embedded Data File Sample.

If the XML data resides in a remote XML file, you would define access to the data by assigning an appropriate URL to the Source attribute as follows:

<XmlDataProvider x:Key="BookData" Source="http://MyUrl" XPath="Books"/>

More Code

How to: Use XML Namespaces in Data Binding

This example shows how to handle namespaces specified in your XMLbinding source.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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