SIGN UP MEMBER LOGIN:    
ARTICLE

XML Data Binding in WPF

Posted by Doug Cook Articles | WPF with C# April 11, 2007
This article describes how to bind XML data using a data template in WPF and XAML. For ease of demonstration, the XML is defined as a resource within the XAML document.
Reader Level:

Overview

WPF provides a number of conveniences in terms of accessing and binding external data sources.  This tutorial shows how you can bind and display XML data using a data template in XAML.  This technique is also known as creating an XML data island.

XML Data Binding

For this example we will create a very simple XAML application that displays a list of favorites (URLs) from an XML data source.  In our XAML document we will define a data source as a resource and supply that resource with our XML data, in this case a list of URLs.  To do this we will use the XMLDataProvider tag.

<!-- define the XML data source as a resource -->
<XmlDataProvider x:Key=
"BookmarkData" XPath="/Favorites">
          <x:XData>
                   <Favorites xmlns=
"">
                             <Bookmark>
                                      <Title>
Google</Title>
                                      <URL>http://www.google.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Amazon</Title>
                                      <URL>http://www.amazon.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Slashdot</Title>
                                      <URL>http://www.slashdot.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Ars Technica</Title>
                                      <URL>http://www.arstechnica.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
New Egg</Title>
                                      <URL>http://www.newegg.com</URL>
                             </Bookmark>
                   </Favorites
>
          </x:XData
>
</XmlDataProvider>
 

As you can see, we have also defined an XPath attribute within the XMLDataProvider tag.  This path definition allows us to access the XML data based on the parent node "Favorites".  Once this has been defined, we need to prepare our bookmark data for display by binding it to a data template.  We can do this using the DataTemplate and Binding tags, also within the resources portion of our XAML document.

<!-- create a data template to display the desired XML node values -->
<
DataTemplate x:Key
="BookmarkDataTemplate">
        <StackPanel Margin
="5">
                <TextBlock FontSize="12" FontWeight="Bold" Foreground
="White">
                        <TextBlock.Text
>
                                <Binding XPath
="Title"/>
                        </TextBlock.Text
>
                </TextBlock
>
                <TextBlock FontSize="10" Foreground
="LightGray">
                        <TextBlock.Text
>
                                <Binding XPath
="URL"/>
                        </TextBlock.Text
>
                </TextBlock
>
        </StackPanel
>
</
DataTemplate
>

A data template is now configured to accept both Title and URL node values, displaying each in its own TextBlock.  For purposes of layout, we have arranged these in a single StackPanel.  The Binding tag and XPath attribute have been used to define the specific XML nodes we are interested in pulling data from.

All that is left is to use the data template to actually write our data to the screen.  We do this by referencing the data template in the main portion of our XAML document.  Here we will bind the data to a standard ListBox control using the ItemsSource and ItemTemplate properties. 

<!-- write the data to the screen by binding the data template to a list box -->
<
StackPanel
>
        <TextBlock FontSize="14" FontWeight="Bold" Margin="10">My Favorites</TextBlock
>
        <
ListBox
                Background
="#999"
                BorderThickness
="2"
                BorderBrush="White"
 
                Margin
="10"
                ItemsSource
="{Binding Source={StaticResource BookmarkData}, XPath=Bookmark}"
                ItemTemplate
="{StaticResource BookmarkDataTemplate}"/>
</
StackPanel> 

The ItemsSource property specifies a binding source referencing the original XML data we defined in the resources portion of our document.  The XPath property is set to "Bookmark", the repeating child node within "Favorites".  Lastly, the value for ItemTemplate is set to refer to the key of our data template, "BookmarkDataTemplate".

Displayed in its entirity, our XAML file now looks like the following:

<Window x:Class="WindowsApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication1" Width="550" Height="400" Name="Form1">

          <Window.Resources>

                    <XmlDataProvider x:Key="BookmarkData" XPath="/Favorites">

                             <x:XData>

                                       <Favorites xmlns="">

                                                <Bookmark>

                                                          <Title>Google</Title>

                                                          <URL>http://www.google.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Amazon</Title>

                                                          <URL>http://www.amazon.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Slashdot</Title>

                                                          <URL>http://www.slashdot.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Ars Technica</Title>

                                                          <URL>http://www.arstechnica.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                         <Title>New Egg</Title>

                                                         <URL>http://www.newegg.com</URL>

                                                </Bookmark>

                                      </Favorites>

                             </x:XData>

                   </XmlDataProvider>

 

                   <!-- create a data template to display the desired XML node values -->

                   <DataTemplate x:Key="BookmarkDataTemplate">

                             <StackPanel Margin="5">

                                      <TextBlock FontSize="12" FontWeight="Bold" Foreground="White">

                                                <TextBlock.Text>

                                                         <Binding XPath="Title"/>

                                                </TextBlock.Text>

                                      </TextBlock>

                                      <TextBlock FontSize="10" Foreground="LightGray">

                                                <TextBlock.Text>

                                                         <Binding XPath="URL"/>

                                                 </TextBlock.Text>

                                      </TextBlock>

                             </StackPanel>

                   </DataTemplate> 

</Window.Resources>

 

 

<!-- write the data to the screen by binding the data template to a list box -->

<StackPanel>

          <TextBlock FontSize="14" FontWeight="Bold" Margin="10">My Favorites</TextBlock>

          <ListBox

               Background="#999"

               BorderThickness="2"

               BorderBrush="White"

               Margin="10"

               ItemsSource="{Binding Source={StaticResource BookmarkData}, XPath=Bookmark}"

               ItemTemplate="{StaticResource BookmarkDataTemplate}"/>

</StackPanel> 

</Window>


When compiled, a window is displayed with a standard ListBox populated with our XML data:

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Doug, can you please let me know how to bind XML Data in metro app? Thanks, Mahesh

Posted by Mahesh Reddy Feb 27, 2012

Very good article.

Posted by Arjun Panwar Nov 25, 2011

nice article,thanks

Posted by Lana Aug 11, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor