We would like to implement existance checks of elements via XPath expression. The XPath expressions are defined externally. We are trying to use the XPathEvaluate extension method from System.Xml.XPath, but it complains when we try to do the following.
XDocument d = new XDocument(
new XElement("test",
new XElement("element"),
new XElement("element")));
bool b = (bool)d.XPathEvaluate("/test/element[exists()]");
This function currently fails with an exception "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.". We get the same message if we try exists(/test/element)
.
One of the other things that we would like to express in an XPath expression is the number of elements, e.g. count(/text/element) = 2
.
The testing condition should be part of the XPath expression.
The function call d.XPathEvaluate("/test/element[last()]")
evaluates as expected.
Is it possible to use XPathEvaluate
this way?
[Edit] Yes.
If not, are there any alternatives in the .Net framework?
[Edit] Yes.
[Edit]
Remaining question: why doesn't the exists
function work?