Search code examples
c#xmlxpath-2.0xpathnavigator

How to use XPath-Functions like exists() in C#?


I want to read some Nodes from a XmlDocument-Object using the SelectNodes-Method and the XPathNavigator-Class.

But C# is unable to evaluate this (validated with XMLSpy) XPath-Expressen:

//LogicUnit[exists(Level[@val = 'R'])]/LogicLines[exists(LogicLine/DATAVIEW_SRC)]

The Runtime throws the XPAthException "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

I don't understand this exception because my XML-Document doesn't use any Namespaces and there is also no XSLT-Document. This exception always appears if I'm using any XPATH-function.


Solution

  • The reason is that it can't use functions without a namespace manager, however, you don't need to use functions, and your code is using that function in the wrong way. you don't need the function exists() to see if something exists, from what I see you are using

    //LogicUnit[exists(Level[@val = 'R'])]
    

    where you mean

    //LogicUnit[Level[@val = 'R']]