Showing posts with label XPath. Show all posts
Showing posts with label XPath. Show all posts

Thursday, November 10, 2011

Binding ListBox with Xml Data using XmlDataProvider


In this post I will demonstrate how to bind xml data with Listbox.

Let’s start with an example.

XAML
<Window x:Class="WpfApplication1.ListBoxWithXmlData"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bind ListBox With XmlData" Height="300" Width="300">
<Window.Resources>
<XmlDataProvider x:Key="myCustomers" XPath="Customers">
    <x:XData>
        <Customers xmlns="">
            <Customer Name="Mitesh Sureja" Phone="443322" />
            <Customer Name="Parag Kumar" Phone="558652" />
            <Customer Name="Kalyan S" Phone="654821" />
            <Customer Name="Subrata Banerjee" Phone="225566" />
            <Customer Name="Rama K" Phone="998855" />
            <Customer Name="Jigar Seth" Phone="774411" />
        </Customers>
    </x:XData>
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<TextBlock Text="- Customer list - " Margin="2"
    HorizontalAlignment="Center"/>
<ListBox Margin="5"
         ItemsSource="{Binding Source={StaticResource myCustomers},XPath=*}">
    <ListBox.ItemTemplate>
        <DataTemplate>
                <WrapPanel Margin="3">
                    <TextBlock Text="{Binding XPath=@Name}"
                    Foreground="Red"
                    Margin="0,0,10,0" />
                    <TextBlock Text="{Binding XPath=@Phone}"
                    Foreground="Green"
                    Margin="5,0,0,0"/>
                </WrapPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
</StackPanel>

Output

As shown in above example, XmlDataProvider named myCustomers contains collection of Customers and this myCustomers xmldataprovider bind with Listbox’s ItemSource property. ListBox also has datatemplate and template contains two textblocks which are bind with Name and Phone property of myCustomers. So when you run this application listbox shows customer’s name and phone number as shown in above snapshot.

Tuesday, November 8, 2011

Binding WPF DataGrid with XML File using XmlDataProvider


In this article I will demonstrate how to bind xml file with WPF datagrid.

There are several ways to achieve this but in this post i will demonstrate using XMLDataProvider to load and bind xml file to WPF Data Grid. XmlDataProvider exposes Source and XPath properties. Source property can be used to provide xml file name while XPath property used to specify the element name to generate collection.

Let’s have a look on below example.

XML File – CustomerData
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <Customer Name="Mitesh Sureja" Address="Address1"
            City="Mumbai" Phone="443322" />
  <Customer Name="Parag Kumar" Address="Address2"
            City="New Delhi" Phone="558652" />
  <Customer Name="Kalyan S" Address="Address3"
            City="Bangalore" Phone="654821" />
  <Customer Name="Subrata Banerjee" Address="Address4"
            City="Calcutta" Phone="225566" />
  <Customer Name="Rama K" Address="Address5"
            City="Chennai" Phone="998855" />
  <Customer Name="Jigar Seth" Address="Address6"
            City="Ahmedabad" Phone="774411" />
</Customers>

XAML File
<Window x:Class="WpfApplication1.DataGridAndXMLData"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataGrid With XmlData" Height="200" Width="300">
    <Window.Resources>
        <XmlDataProvider Source="Data\CustomerData.xml"
                         XPath="Customers" x:Key="custData" />
    </Window.Resources>
    <Grid>
        <DataGrid Name="CustomerGrid" AutoGenerateColumns="False"
                  ItemsSource="{Binding Source={StaticResource custData},XPath=*}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding XPath=@Name}"
                                    Header="Name" />
                <DataGridTextColumn Binding="{Binding XPath=@Address}"
                                    Header="Address" />
                <DataGridTextColumn Binding="{Binding XPath=@City}"
                                    Header="City" />
                <DataGridTextColumn Binding="{Binding XPath=@Phone}"
                                    Header="Phone" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Output


As demonstrated in above example, XmlDataProvider loads CustomerData.xml file. Data Grid’s Item source property is bind with custData key and each column is bind with XPath. The similar thing can also be achieved using LinqToXML(XLINQ).

See Also –