Closed SpartanNico94 closed 3 years ago
@SpartanNico94 Can you provide a full example please?
@SpartanNico94 Can you provide a full example please?
@StefH, here is an example. There are comments to help you understand better the problem. I solved using xpath functions such as substring() etc or .XPath2SelectNode().InnerText in order to get the value of the node
using System;
using System.Xml;
using Wmhelp.XPath2;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string XmlTest = "<start>"
+ "<node1>Value1</node1>"
+ "<node2>"
+ "<subnode1>Value2</subnode1>"
+ "</node2>"
+ "</start>";
XmlDocument Xml = new XmlDocument();
Xml.LoadXml(XmlTest);
string value = Xml.XPath2Evaluate("/start/node2/subnode1").ToString();
//!!the pipe (|) is used to see the space in console!!
Console.WriteLine($"|{value}|"); //value is "Value2, " (without double quotes but with space)
Console.ReadKey();
}
}
}
I see.
However I think you should use:
static void Main(string[] args)
{
string XmlTest = "<start>"
+ "<node1>Value1</node1>"
+ "<node2>"
+ "<subnode1>Value2</subnode1>"
+ "</node2>"
+ "</start>";
var doc = new XmlDocument();
doc.LoadXml(XmlTest);
XPathNavigator navigator = doc.CreateNavigator();
var xpath = "string(/start/node2/subnode1)";
string value = (string) navigator.Evaluate(xpath);
Console.WriteLine($"XPath = |{value}|");
string value2 = (string) navigator.XPath2Evaluate(xpath);
Console.WriteLine($"XPath2 = |{value2}|");
}
Which returns:
XPath = |Value2|
XPath2 = |Value2|
The issue with the ,
is here:
https://github.com/StefH/XPath2.Net/blob/master/src/XPath2/XPath2NodeIterator.cs#L177
I'll take a look.
I think you should add a line of code which remove the last [, ] "token" in order to get a well formatted string.
Thanks for your help.
When using .NET function XPath2Evalute with a command as "//Node1/Element1" and converting to string I always get a string like this "< value >, " (with comma AND space in string); but if I use a function as substring(), string-join() or number() I get the correct node value.
To avoid this behaviour I have to use concat( < command >, '') and I get the node value as is written in the xml file.
Is this the expected behavior or it is a bug?