Search code examples
wpfxamlxmldataprovider

Displaying a specific node from an XML file, on a textbox


I have an xml file that looks something like this

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

How do I display a specific node on a textbox control? Currently I'm trying to show the second "NumberID" from my XML file:

<Window.Resources>
        <XmlDataProvider x:Key="RoutingData"
               Source="/RoutingLogic.xml"
               XPath="Data/NumberID[2]"/>         
</Window.Resources>

...

<TextBox Text="{Binding ElementName=RoutingData}">

But it's now showing anything. What am I doing wrong?


Solution

  • I figured it out. I changed the binding info to this:

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

    ...

    <TextBox Text="{Binding Source={StaticResource RoutingData}, XPath=NumberID[2]}"/>