CoolStraw
CoolStraw

Reputation: 5390

WPF Datagrid binding to xml

I want to programmatically bind an XML file containing some results pulled out from database to my Datagrid. My XML looks like:

<root>
    <resultset>
        <header>
            <column> Col 1 </column>
            <column> Col 2 </column>
        </header>

        <data>
            <row>
               <field>Value field 1</field>
               <field>Value field 2</field>
            </row>
            <row>
               <field>Value field 1</field>
               <field>Value field 2</field>
            </row>
        </data>
    </resultset>
</root>

I'm trying to map the header to the datagrid header, and the rows to the rows... The only thing is that I'm kinda stuck, I'm lost among the programmatic properties, could someone just give me a hint to what to look after?

I've tried doing:

myDataGrid.ItemsSource = myXmlDoc

But it doesn't really help nothing shows up hehe, I've read about some "path" binding property but I can't find it.

Upvotes: 0

Views: 4042

Answers (2)

Arseny
Arseny

Reputation: 7351

I suppose you already have xmldataprovider with your Xml source named myXMLDoc. then you can bind xml data to your WPF controls.

ItemsSource="{Binding Source={StaticResource myXMLDoc}, XPath=row}}" //bind "row" elements to your control

Upvotes: 1

Rei Miyasaka
Rei Miyasaka

Reputation: 7096

Is this what you're looking for?

http://joshsmithonwpf.wordpress.com/2007/06/04/binding-to-xml/

I think what you want to do is hook up ItemsSource to an XmlDataProvider (which you can also create in code), with its XPath set to /root/resultset/data/row. Then for each element you can use, say Text = {Binding XPath=./field}.

I'm a little iffy on the details since I haven't done this in a while, but hopefully that'll set you in the right direction.

Upvotes: 1

Related Questions