1

I'm trying to create a WPF treeview based on the output from an xpath query. The idea is that, based on a search string, a treeview representing a tiny subset of a very large xml document will be presented. WPF and xpath are totally new to me (couple of weeks) so possibly a dumb question...

In a console application, this works fine and gives exactly what I want as a basis for the tree.

XmlNodeList nodeList = root.SelectNodes("//item[not (item) and contains(name, 'kidney')]//ancestor::item");

foreach (XmlNode node in nodeList)
{
    file.WriteLine($"{node.ChildNodes[0].InnerText}");

    //Console.WriteLine($"{node.Name} -  {node.InnerText}");
}

However, when I use the same xpath in a binding as follows:

ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=//item[not (item) and contains(name, 'kidney')]//ancestor::item}"

I get a bunch or errors around the construction of the xpath piece (xpath = /* works fine). I thought it might be the single quotes but replacing with the XML special character or escaping with \ makes no difference. At this stage I'm wondering if nested xpath statements can be used at all?

har07
  • 88,338
  • 12
  • 84
  • 137
woody
  • 29
  • 4
  • Do it the easy way. Don't use bind, instead just add the items int a treeview. Use your console code, but instead of using WriteLine() add strings to a treeview. – jdweng May 27 '16 at 00:21
  • Ah... All of the examples I've looked use binding in the xaml. If I'm understanding you the treeview can be managed in c# code? That would suit me much better so I'll research that. Thanks a lot. – woody May 27 '16 at 10:23
  • See following webpage : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp. The code recursively puts an entire xml file into a treeview but you can modify the code as required. – jdweng May 27 '16 at 10:25
  • Thanks, I'll try that over the weekend – woody May 27 '16 at 11:11

1 Answers1

0

Try to escape ' in your XPath binding-parameter as follow :

XPath=//item[not(item) and contains(name, \'kidney\')]//ancestor::item

Similar problem : Error in XPath binding in XAML

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137