0

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.

Noam M
  • 3,156
  • 5
  • 26
  • 41
CoolStraw
  • 5,282
  • 8
  • 42
  • 64

2 Answers2

1

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.

Rei Miyasaka
  • 7,007
  • 6
  • 42
  • 69
  • THank you for your answer Rei. The thing is that I want to achieve this **programmatically** (can't do it in XAML due to app purpose/arch.). And the thing is that in a programmatic way I can't use binding expressions anymore that's why I'm getting kinda lost among all these properties, I'm reading a MSN article about data binding but it's still confusing. (**new** to wpf by the way :p) – CoolStraw Nov 23 '10 at 09:49
1

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
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Arseny
  • 7,251
  • 4
  • 37
  • 52
  • WOW I didn't know I could put expressions programmatically... dude that's awesome ! thank you very much. It gives me the rows (but empty yet) but it's better than what I had before I'm gonna dig to figure out what's wrong. Again, thank you – CoolStraw Nov 23 '10 at 09:53
  • Just one question, How do you do to bind the tags to the datagrid header columns ? I tried looking at mydatagrid.columns but it's a collection, it doesn't really have ItemsSource or something. Thank you :) – CoolStraw Nov 23 '10 at 09:56
  • @Miloud check it out http://stackoverflow.com/questions/320089/how-do-i-bind-a-wpf-datagrid-to-a-variable-number-of-columns – Arseny Nov 23 '10 at 10:07
  • Indeed I've found that one and it works :D Thanks man :). Only one thing, my columns are set right, but my rows are empty.. How can I map each in to each column ? – CoolStraw Nov 23 '10 at 10:28