1

I have an xml file named Data.xml and it looks something like this

<Data>
  <NumberID>23423 </NumberID>
  <NumberID>34234 </NumberID>
  <NumberID>45435 </NumberID>
  <NumberID>67867 </NumberID>
</Data>

I want to display these numbers, on a ListBox control on my application. How can I bind this xml file to the list?

H.B.
  • 166,899
  • 29
  • 327
  • 400
MVZ
  • 2,220
  • 5
  • 27
  • 57

1 Answers1

2

Use an XmlDataProvider:

<Window.Resources>
   <XmlDataProvider x:Key="Items" Source="Data.xml" XPath="Data/NumberID" />
</Window.Resources>

...

<ListBox ItemsSource="{Binding Source={StaticResource Items}}" />
JohnB
  • 13,315
  • 4
  • 38
  • 65
  • It works but now I have another problem. NumberID sometimes have some child elements, so the ListBox shows, the numbers, followed by the content of the child elements. – MVZ May 26 '12 at 23:51
  • You could try (not tested) – JohnB May 27 '12 at 09:38
  • 1
    No, that does not do what it should. Append text() to the XPath within the XmlDataProvider: That works. – JohnB May 27 '12 at 10:31