Hello..I am new to WPF .I need to bind a simple xml file. I want to display the tags in my list box.I have written the following code,but not understanding where am I going wrong. Please help.
Code:
//XML file Bookmarks.xml
<?xml version="1.0" ?>
<Favourite>
<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>
</Favourite>
//.xaml
<Window x:Class="XMLFileBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="dataProvider" Source="C:\Documents and Settings\MyFolder\Desktop\Bookmarks.xml" >
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<TextBlock
FontSize="14"
FontWeight="Bold"
Margin="10">
My Favorites
</TextBlock>
<ListBox
Background="#999"
BorderThickness="2"
BorderBrush="White"
Margin="10"
ItemsSource="{Binding {StaticResource dataProvider}, XPath=/Favourite/Bookmark}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock
FontSize="12"
FontWeight="Bold"
Foreground="White"
Text="{Binding XPath=@Title}"/>
<TextBlock FontSize="10"
Foreground="LightGray"
Text="{Binding XPath=@URL}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>