XPathExpression.AddSort Method (Object, XmlSortOrder, XmlCaseOrder, String, XmlDataType)

 

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters.

Namespace:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public abstract void AddSort(
	object expr,
	XmlSortOrder order,
	XmlCaseOrder caseOrder,
	string lang,
	XmlDataType dataType
)

Parameters

expr
Type: System.Object

An object representing the sort key. This can be the string value of the node or an XPathExpression object with a compiled XPath expression.

order
Type: System.Xml.XPath.XmlSortOrder

An XmlSortOrder value indicating the sort order.

caseOrder
Type: System.Xml.XPath.XmlCaseOrder

An XmlCaseOrder value indicating how to sort uppercase and lowercase letters.

lang
Type: System.String

The language to use for comparison. Uses the CultureInfo class that can be passed to the Compare method for the language types, for example, "us-en" for U.S. English. If an empty string is specified, the system environment is used to determine the CultureInfo.

dataType
Type: System.Xml.XPath.XmlDataType

An XmlDataType value indicating the sort order for the data type.

Exception Condition
XPathException

The XPathExpression or sort key includes a prefix and either an XmlNamespaceManager is not provided, or the prefix cannot be found in the supplied XmlNamespaceManager.

  • The order in which the sorts are added provides the sort key order.

  • If the XPathExpression or the sort key requires namespace resolution, you must use the SetContext method to provide an XmlNamespaceManager for namespace resolution.

  • If the XPathExpression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. If your XML includes a default namespace, you must still use the SetContext method and provide an XmlNamespaceManager that contains a prefix and namespace URI to handle the default namespace.

The following example shows how to sort the books document by price in descending order.

using System;
using System.Xml;
using System.Xml.XPath;

namespace SortBooks
{
    class Program
    {
        static void Main(string[] args)
        {
            XPathDocument doc = new XPathDocument("contosoBooks.xml");
            XPathNavigator nav = doc.CreateNavigator();

            XPathExpression expr;
            expr = nav.Compile("/bookstore/book");

            expr.AddSort("price", XmlSortOrder.Descending,
                           XmlCaseOrder.None, "", XmlDataType.Number);

            XPathNodeIterator iterator = nav.Select(expr);
            while (iterator.MoveNext())
            {
                if (iterator.Current.HasChildren)
                {
                    XPathNodeIterator childIter =
              iterator.Current.SelectChildren(XPathNodeType.Element);
                    while (childIter.MoveNext())
                    {
                        Console.WriteLine(childIter.Current.Value);

                    }
                }
            }
        }
    }
}

The example takes the books.xml file as input.


<?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>

.NET Framework
Available since 1.1
Silverlight
Available since 4.0
Return to top
Show: