1
是否可以使用XPathExpression.AddSort對元素進行排序而不僅僅是對節點進行排序?XPathExpression AddSort
如果我更改了XPathExpression.AddSort的MSDN文檔中的示例代碼,所以我請求了一個元素而不是整個節點,排序順序不起作用。
var doc = new XPathDocument("contosoBooks.xml");
var nav = doc.CreateNavigator();
var expr = nav.Compile("/bookstore/book/title");
expr.AddSort("title", XmlSortOrder.Descending,
XmlCaseOrder.None, "", XmlDataType.Number);
var iterator = nav.Select(expr);
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current);
}
我希望這樣的輸出:
The Gorgias
The Confidence Man
The Autobiography of Benjamin Franklin
但輸出是這樣的:
The Autobiography of Benjamin Franklin
The Confidence Man
The Gorgias
這是給你參考的XML文件
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
除了下面的回答(你應該的方式接受),你也應該指定正確的' XmlDataType'應該是'XmlDataType.Text'用於基於字符串的排序。 –