For an ASP.NET project I wrote a controller action and a form where the user can enter an xpath expression and retrieve the result from an xml file on the server. It works fine, even with some string functions like concat, substring-before and substring-after.

To get rid of some commas in the output, I tried to use string-join and tokenize. However, by using one of these functions I end up with an XPathException.

XPathDocument doc = new XPathDocument(@"C:\temp.xml");
XPathNavigator navigator = doc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("x", "systemInfo");
var temp = navigator.Evaluate("string-join(tokenize('The quick brown fox', ' '), ';')", nsmgr); // Exception

Did I use these xpath functions not correctly?

有帮助吗?

解决方案

string-join and tokenize are XPath 2.0 functions, but .net's XPathNavigator only supports XPath 1.0. You cannot use these functions, and there is no XPath 1.0 equivalent for them.

Either perform string manipulation outside XPath in C# or use some library that extends the capabilities, you might want to have a look at Saxon or BaseX which both offer APIs for C#. There are also some more of them, both open source and commercial ones.

An XPath 1.0 hack for this particular example: translate('The quick brown fox', ' ', ';') will replace all occurrences of spaces with semicolons..

其他提示

You would need to look into third party solutions like the .NET version of Saxon 9 or like XmlPrime. XmlPrime's API is close to the original Microsoft one, see http://www.xmlprime.com/xmlprime/doc/2.9/T_XmlPrime_ExtensionMethods_XPathNavigatorExtensions.htm which adds extension methods to allow XPath 2.0 evaluation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top