HOW TO: Use the AddSort Method of the XPathExpression Object in Visual C# .NET (318542)



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft XML Classes (included with the .NET Framework 1.0)
  • Microsoft XML Classes (included with the .NET Framework 1.1)

This article was previously published under Q318542

SUMMARY

This step-by-step article demonstrates how to use the AddSort method of the System.Xml.XPath.XPathExpression class to sort the results of an XML Path Language (XPath) query that is based on a specified element or attribute.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
This article assumes that are familiar with the following topics:
  • Visual C# language
  • XML standards
  • XPath query language
back to the top

Create the Sample XML Document

  1. In Notepad, create a new XML document that contains the following code:
    <?xml version='1.0'?>
    <Books>
    <Book>
      <Title>Advanced XML</Title>
      <Publisher>Lucerne Publishing</Publisher>
    </Book>
    <Book>
      <Title>Learn XML Today</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    <Book>
      <Title>XML for Gurus</Title>
      <Publisher>Lucerne Publishing</Publisher>
    </Book>
    <Book>
      <Title>Developing XML solutions</Title>
      <Publisher>MSPress</Publisher>
    </Book>
    </Books>
    					
  2. Save the document as Books.xml in the root folder of your hard disk.
back to the top

Create the Sample Visual C# .NET Application

  1. Follow these steps to create a new Visual C# .NET Windows Application project:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.
  2. Drag a Button control from the toolbox to Form1.cs.
  3. Double-click the button in Design view. In the Click event procedure, add the following code to create a handler for the Click event of the Button control:
    // Construct the XPathDocument by specifying the path to Books.xml.
    System.Xml.XPath.XPathDocument xmldoc = new System.Xml.XPath.XPathDocument("c:\\books.xml");
    
    // Create the XPathNavigator.
    System.Xml.XPath.XPathNavigator nav = xmldoc.CreateNavigator();
    
    // Compile the XPath query expression to select all the Title elements.
    // The Compile method of the XPathNavigator generates an XPathExpression 
    // object that encapsulates the compiled query.
    System.Xml.XPath.XPathExpression expr  = nav.Compile("/Books/Book/Title");
    
    // Execute the AddSort method of the XPathExpression object to define the 
    // Title Element as the sort key.
    expr.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending, System.Xml.XPath.XmlCaseOrder.None, "", System.Xml.XPath.XmlDataType.Text);
    
    
    // Create the XPathNodeIterator by executing the Select method of the 
    // XPathNavigator. Notice that the XPathExpression object is supplied as 
    // the query expression parameter.
    System.Xml.XPath.XPathNodeIterator iterator  = nav.Select(expr);
    
    System.Diagnostics.Debug.WriteLine("Titles sorted in Ascending order...");
    System.Diagnostics.Debug.WriteLine("***********************************");
    
    // Use the iterator to explore the result set that is generated.
    while (iterator.MoveNext())
    {
       System.Diagnostics.Debug.WriteLine(iterator.Current.Value);
    }
    					
  4. Read the inline comments to understand the functionality of the code. Notice the line of code that executes the AddSort method of the XPathExpression object.
    public abstract void AddSort(
       object expr,
       XmlSortOrder order,
       XmlCaseOrder caseOrder,
       string lang,
       XmlDataType dataType
    );
    						
    The following is a description of the parameters in the overload of the AddSort method that are used in this sample:
    
    expr         An expression that represents the sort key. This can be a 
                 string or an XPathExpression object. The result of this 
                 expression is converted to a string, according to the XPath 
                 specification. In an XSLT style sheet, if you use xsl:sort but 
                 do not specify a select expression, string(.) is used by 
                 default. 
    
    order        A System.Xml.XPath.XmlSortOrder enumeration value that 
                 indicates the sort order. 
    
    caseOrder    A System.Xml.XPath.XmlCaseOrder enumeration value that 
                 indicates how to sort uppercase and lowercase letters. This is 
                 language-dependent if you supply a lang parameter. 
    
    lang         The language to use for comparison. Uses the CultureInfo class 
                 that you can pass to the String.Compare method for the 
                 language types (for example, "us-en" for U.S. English). If you 
                 specify an empty string, the system environment is used to 
                 determine the CultureInfo. 
    
    dataType     System.Xml.XPath.XmlDataType enumeration value that indicates 
                 sort order for a data type. 
    					
back to the top

Test the Sample Code

  1. Save the changes to the Visual C# .NET project, and then run the project.
  2. When the form appears, click the button to execute the code. A list of titles appears in the Visual Studio .NET Output window, and the titles are sorted in ascending order:
    Titles sorted in Ascending order...
    ***********************************
    Advanced XML
    Developing XML solutions
    Learn XML Today
    XML for Gurus
    					
back to the top

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

313828 INFO: Roadmap for Executing XPath Queries in .NET Applications

back to the top

Modification Type:MajorLast Reviewed:9/22/2003
Keywords:kbHOWTOmaster KB318542 kbAudDeveloper