XPath DOM 编程中有什么方法可以使用System.Xml
to run selectNodes
(XPATH)带有排序参数?
例如,对于以下 XML,程序按照与文档相同的顺序(降序)写入值。有没有办法使用 XPath 来按升序获取值?
笔记。当然,您可以在 XSL 中进行预排序,但是我需要在循环访问这些值时更新这些值。由于 XSL 给了我一个排序的copy的元素,不是实际的元素本身,我无法使用XSL。
这是一些 XML,一个程序输出
public static void Main() {
XmlDocument xml = new XmlDocument();
xml.Load( "t.xml" );
// SelectNodes gets in document order, but I want in
// ascending order based on @value
foreach( XmlNode ndNode in xml.SelectNodes( "/xml/ele" ) ) {
Console.WriteLine( ndNode.Attributes["value"].Value );
}
}
这是 XML
<xml>
<ele value='3' test='Third'/>
<ele value='2' test='Second'/>
<ele value='1' test='First'/>
</xml>
最后按文档(降序)顺序输出。我想要一个按升序返回节点的 XPath。
3
2
1
PS,我正在使用System.Xml
在 Visual Studio 2008 .NET 3.5 中
XPath 不支持排序,但是您可以查看AddSort https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathexpression.addsort
method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)