Posted by: Zeeshan Amjad | October 17, 2009

Binding XML Data

We can also bind the XML data with UI elements. Lets take a first step to define the XML data in WPF program. We can define the XML data using XmlDataProvider class. This class is inherited by abstract class named DataSourceProvider. DataSourceProvide is a wrapper on data model and has a property Dispatcher. It means this class can work with WPF threading model. There are two classes inherited by this class named ObjectDataProvider and XmlDataProvider. Here is a class diagram of these classes.


This class can also be used in XAML. Here is a code to use this class in XAML.

  1: <XmlDataProvider x:Key="StatesInformation" XPath="States">
  2:     <x:XData>
  3:         <States xmlns="">
  4:             <State Name="Maryland" Capital="Annapolis"/>
  5:             <State Name="California" Capital="Sacramento"/>
  6:             <State Name="Taxes" Capital="Austin"/>
  7:             <State Name="Washington" Capital="Olympia"/>
  8:         </States>
  9:     </x:XData>
 10: </XmlDataProvider>        

We define this in windows Resource section and use XPath property of data binding class to perform binding. Here is a complete XAML code to demonstrate this.

  1: <Window x:Class="xmlDataBinding.Window1"
  2:     xmlns=""
  3:     xmlns:x=""
  4:     Title="XML Data Binding" Height="300" Width="400">
  5:     <Window.Resources>
  6:         <XmlDataProvider x:Key="StatesInformation" XPath="States">
  7:             <x:XData>
  8:                 <States xmlns="">
  9:                     <State Name="Maryland" Capital="Annapolis"/>
 10:                     <State Name="California" Capital="Sacramento"/>
 11:                     <State Name="Taxes" Capital="Austin"/>
 12:                     <State Name="Washington" Capital="Olympia"/>
 13:                 </States>
 14:             </x:XData>
 15:         </XmlDataProvider>        
 16:     </Window.Resources>
 18:     <ListBox Margin="5" HorizontalContentAlignment="Stretch"
 19:         ItemsSource="{Binding Source={StaticResource StatesInformation}, 
 20:         XPath=State}">
 21:         <ListBox.ItemTemplate>
 22:             <DataTemplate>
 23:                 <Border Margin="5" BorderThickness="3" BorderBrush="Navy" CornerRadius="5" Background="Beige">
 24:                     <StackPanel Margin="5">
 25:                         <TextBlock FontWeight="Bold" Text="{Binding XPath=@Name}"/>
 26:                         <TextBlock Text="{Binding XPath=@Capital}"/>
 27:                     </StackPanel>
 28:                 </Border>
 29:             </DataTemplate>
 30:         </ListBox.ItemTemplate>
 31:     </ListBox>
 32: </Window>

Here is the output of this program.




  1. great going zee,

    you are near to publish a book …

    are you using WPF professionally ?

    • Thanks ghaffar to like it. I cant say anything about book right now, but i am not using WPF professionaly. The only thing i am doing is giving traning of WPF to my team in my offic.e

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: