是否有可用于连接多个属性值并与 XPathNavigator.Evaluate 一起使用的 xpath 表达式
<root>
<node class="string"></node>
<node class="join"></node>
</root>
XPathNavigator.Evaluate(<expression>)
should return a string with value string;join
谢谢。
最佳答案
这样的事情应该没问题:
var document = XDocument.Parse(s);
var res = (document.Root.XPathEvaluate("/root/node/@class") as IEnumerable).Cast<XAttribute>().Aggregate("", (a, c) => a + ";" + c.Value);
res = res.Substring(1);
在 XPath 2.0 中有一个更好的选项,带有 string-join 但不确定它是否在 .Net 中实现...
编辑:否则动态构建 XPath 表达式:
int count = (document.Root.XPathEvaluate("/root/node") as IEnumerable).Cast<XNode>().Count();
string xpath = "concat(";
for (int i = 1; i <= count; ++i)
{
xpath += "/root/node[" + i + "]/@class";
if (i < count)
{
xpath += ", ';',";
}
else
{
xpath += ")";
}
}
var res = document.Root.XPathEvaluate(xpath);
关于c# - 字符串在c#中的xpath中连接多个属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16973853/