XPathExpression.AddSort Method

Definition

When overridden in a derived class, sorts the nodes selected by the XPath expression.

Overloads

AddSort(Object, IComparer)

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.

AddSort(Object, XmlSortOrder, XmlCaseOrder, String, XmlDataType)

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

AddSort(Object, IComparer)

Source:
XPathExpr.cs
Source:
XPathExpr.cs
Source:
XPathExpr.cs

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.

public:
 abstract void AddSort(System::Object ^ expr, System::Collections::IComparer ^ comparer);
public abstract void AddSort (object expr, System.Collections.IComparer comparer);
abstract member AddSort : obj * System.Collections.IComparer -> unit
Public MustOverride Sub AddSort (expr As Object, comparer As IComparer)

Parameters

expr
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.

comparer
IComparer

An IComparer object that provides the specific data type comparisons for comparing two objects for equivalence.

Exceptions

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.

Remarks

The AddSort method enables users to sort objects by their data type instead of by string or number. The IComparer object provides an implementation of the Compare method that supports sorting on user-defined classes.

In the following example, the books are sorted by ISBN number, where isbn is an object that implements the IComparer interface.

Dim expression As XPathExpression = navigator.Compile("bookstore/book")  
Dim isbn As ISBN = New ISBN()  
expression.AddSort("@ISBN", (IComparer)isbn)  
XPathExpression expression = navigator.Compile("bookstore/book");  
ISBN isbn = new ISBN();  
expression.AddSort("@ISBN", (IComparer)isbn);  

The following are important notes to consider when using the AddSort method.

  • 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.

See also

Applies to

AddSort(Object, XmlSortOrder, XmlCaseOrder, String, XmlDataType)

Source:
XPathExpr.cs
Source:
XPathExpr.cs
Source:
XPathExpr.cs

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

public:
 abstract void AddSort(System::Object ^ expr, System::Xml::XPath::XmlSortOrder order, System::Xml::XPath::XmlCaseOrder caseOrder, System::String ^ lang, System::Xml::XPath::XmlDataType dataType);
public abstract void AddSort (object expr, System.Xml.XPath.XmlSortOrder order, System.Xml.XPath.XmlCaseOrder caseOrder, string lang, System.Xml.XPath.XmlDataType dataType);
abstract member AddSort : obj * System.Xml.XPath.XmlSortOrder * System.Xml.XPath.XmlCaseOrder * string * System.Xml.XPath.XmlDataType -> unit
Public MustOverride Sub AddSort (expr As Object, order As XmlSortOrder, caseOrder As XmlCaseOrder, lang As String, dataType As XmlDataType)

Parameters

expr
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
XmlSortOrder

An XmlSortOrder value indicating the sort order.

caseOrder
XmlCaseOrder

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

lang
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
XmlDataType

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

Exceptions

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.

Examples

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

Imports System.Xml
Imports System.Xml.XPath

Module Module1

    Sub Main()
        Dim doc As New XPathDocument("contosoBooks.xml")
        Dim nav As XPathNavigator = doc.CreateNavigator()

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

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

        Dim iterator As XPathNodeIterator = nav.Select(expr)
        Do While iterator.MoveNext()

            If (iterator.Current.HasChildren()) Then
                Dim childIter As XPathNodeIterator = _
                iterator.Current.SelectChildren(XPathNodeType.Element)
                Do While childIter.MoveNext()
                    Console.WriteLine(childIter.Current.Value)
                Loop

            End If
        Loop

    End Sub

End Module
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>  

Remarks

  • 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.

See also

Applies to