Binding XML data to a datagrid

Dec 2, 2008 at 1:13 PM
Hi everyone,

I've got a newbie question regarding XML data binding. I'm trying to create a basic datagrid sample under Blend (using the WPF Toolkit). I'd like to bind my data grid to data coming from a XML file.

Here is a sample of my XML file:
    <Item Name="Item 1">
        <Name>My first item<Name/>
        <Type>Type A</Type>
    <Item Name="Item 2">
        <Name>My second item<Name/>
        <Type>Type B</Type>

I've created a XmlDataProvider to link my data file to my Blend project. Now I'd like to create a grid with two columns, Name and Type, populated with rows read from my XML file. I didn't figure out how to auto-generate grid columns.

I've also tried to define two DataGridTextColumn into my grid but again I didn't figure out how to bind each column to an XML path.

What should I do?

PS: I can change the XML content & node hierarchy if needed.
Dec 3, 2008 at 4:18 AM

 Take a look at this example of how to bind to XML:

With you data, an XPath of "//item" in th XMLDataProvider would make sense, then column XPath bindings of "Name" and "Type". If you cannot get this to work - perhaps you could show us your XAML file also?

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples

Dec 3, 2008 at 2:55 PM
Thank for your advice Colin.

In fact, I've found all answers to my question in this thread: