Displaying data from an XML file, on a listbox

3.7k Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

Use an XmlDataProvider:

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

...

<ListBox ItemsSource="{Binding Source={StaticResource Items}}" />