在 XPath DOM 编程中是否有任何方法可以使用 System.Xml
运行带有排序参数的 selectNodes
(XPATH)?
例如,对于以下 XML,程序会按照与文档相同的顺序(降序)写入值。有没有办法使用 XPath 以升序获取值?
注意。当然,您可以在 XSL 中进行预排序,但是我需要在遍历这些值时更新这些值。由于 XSL 为我提供了元素的排序副本,而不是实际元素本身,因此我无法使用 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,我在 Visual Studio 2008 .NET 3.5 中使用 System.Xml
最佳答案
XPath 不支持排序,但是您可以查看 <a href="https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathexpression.addsort" rel="noreferrer noopener nofollow">AddSort</a>
方法。
关于c# - 使用 XPath 排序 - 不是 XSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480673/