InnerXml parsing

<TextBlock

DataContext="{Binding Source={StaticResource xmlTrees}}"

Text ="{Binding Path=InnerXml}" >

</TextBlock>

The above XAML produces the string:

<species Name="Red" Scientific="Acer rubrum"/>

<!-- red maple -->

Now, it took me some time to produce that but what I really want to produce is the text of the 'Scientific' attribute.

Can I do that entirely with XAML or do I have to parse the InnerXml in the code-behind or something else

All clues appreciated.



Answer this question

InnerXml parsing

  • Andy Burns

    You can use the XPath property of the TextBox's Binding, instead of the Path. You can set it to something like XPath=species/@Scientific in the XAML. If that doesn't work out, you can always create a value converter and set the Binding's Converter to an instance of that object (again, in XAML). Check out the IValueConverter interface for more info about that.

  • RexH

    Is there any reason you cannot use XPath instead of Path i.e.

    <TextBlock
    DataContext="{Binding Source={StaticResource xmlTrees}}"
    Text ="{Binding XPath=species/@Scientific}" >
    </TextBlock>


  • crowsfoot

    Thank you -- that works!!

    I thought that I had tried all possible combinations of Path and XPath and that's a lot -- maybe a case-sensitive error. In any case, I can move foreward now.


  • InnerXml parsing