How do I bind to XML data in WPF?

WPF has good built-in support for binding to different kinds of data, including XML. In this very simple example we're going to declare a small "island" of XML data in our UI, and bind some text properties to parts of the XML document. Then we'll look at binding to XML documents loaded from other sources. 

We declare the XML data island by adding an XmlDataProvider to the resources collection of our window. Assigning a key to the resource is necessary so we can access it later, and setting an XPath expression specifies the list of nodes the XmlDataProvider will expose. The built-in support for using XPath when binding to XML data is a powerful feature of XML data binding in WPF, and should feel fairly natural for those familiar with XSLT. In this case the expression says to return all the Employee nodes located under a root Employees node. Inside the XmlDataProvider we add an XData node (this is a requirement for XML data islands). Inside the XData node we place any XML content we wish. For this example we've defined a simple set of XML data - a list of employees with one item in the list, Microsoft CEO Steve Ballmer.

<Window.Resources>
    <XmlDataProvider x:Key="EmployeeData" XPath="/Employees/Employee">
      <x:XData>
        <!-- xml data island -->
        <Employees >
          <Employee Name="Steven Ballmer" DOB="1-Mar-1956">
            <Title>CEO</Title>
          </Employee>
        </Employees>

      </x:XData>
    </XmlDataProvider>
</Window.Resources>

To bind to this XML in our application code we use a binding syntax similar to the one used to bind to objects. First we set the DataContext for the panel so that all the child Xaml elements are binding to the same data. This is done using the {StaticResource} markup extension with a key, the key we previously gave our XmlDataProvider. Inside the panel there are two text blocks that will be bound to parts of the Employees XML document. Once again the syntax for binding is similat to the syntax used to bind to objects. We use the {Binding} markup extension, but instead of setting a Path, we specify an XPath expression. To bind to the Name attribute on an employee element the expression is @Name, but more complex espressions are possible such as the second example which binds to the inner text of the title element nested under the employee element.

<!-- binding to data island -->
<StackPanel DataContext="{StaticResource EmployeeData}">
    <TextBlock Text="{Binding [email protected]}" FontSize="30" />
    <TextBlock Text="{Binding XPath=Title/text()}" FontSize="30" />
</StackPanel>

In the real world it is more likely that XML data will be retrieved from an external source rather than imbedded in the UI. This is also easy to achieve using the XmlDataProvider and setting the Source. This example shows how to retrieve the RSS feed from LearnWPF, but XML can also be loaded from the file system or imbedded XML documents.

<XmlDataProvider x:Key="LearnWPFFeed"
                     Source="
http://learnwpf.com/Rss.ashx "  
                     XPath="//item" />

                    
Binding to nodes retrieved from an external source is the same as when we bound to local XML data.

<!-- binding to RSS feed -->
<StackPanel DataContext="{StaticResource LearnWPFFeed}">
  <TextBlock Text="{Binding XPath=title/text()}" FontSize="30" />
  <TextBlock Text="{Binding XPath=link/text()}" FontSize="12" />
</StackPanel>

Simple UI showing bound data from XML sources

The full source for this window with both the data sources is here.