Is it possible to sort the elements and not only the nodes using XPathExpression.AddSort?
If I change the example code on the MSDN documentation for XPathExpression.AddSort a little so I request an element and not the whole node the sort order does not work.
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);
}
I would expect this output:
The Gorgias
The Confidence Man
The Autobiography of Benjamin Franklin
But the output is this:
The Autobiography of Benjamin Franklin
The Confidence Man
The Gorgias
Here is the xml file for your reference
<?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>
It looks like a duplicate question – see XPathExpression AddSort fails
So in your case code has to be like:
or