Microsoft KB Archive/318542

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

Article ID: 318542

Article Last Modified on 9/22/2003

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q318542



For a Microsoft Visual Basic .NET version of this article, see 317084.

In This Task

 * SUMMARY
 * REFERENCES


 * Requirements
 * Create the Sample XML Document
 * Create the Sample Visual C# .Net Application
 * Test the Sample Code



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
  In Notepad, create a new XML document that contains the following code:    Advanced XML Lucerne Publishing   Learn XML Today MSPress</Publisher> </Book>  XML for Gurus</Title> Lucerne Publishing</Publisher> </Book>  Developing XML solutions</Title> MSPress</Publisher> </Book> </Books> </li> <li>Save the document as Books.xml in the root folder of your hard disk.</li></ol>

back to the top

Create the Sample Visual C# .NET Application
<ol> <li>Follow these steps to create a new Visual C# .NET Windows Application project: <ol style="list-style-type: lower-alpha;"> <li>Start Microsoft Visual Studio .NET.</li> <li>On the File menu, point to New, and then click Project.</li> <li>In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.</li></ol> </li> <li>Drag a Button control from the toolbox to Form1.cs.</li> <li> 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(&quot;c:\\books.xml&quot;);

// 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(&quot;/Books/Book/Title&quot;);

// Execute the AddSort method of the XPathExpression object to define the // Title Element as the sort key. expr.AddSort(&quot;.&quot;, System.Xml.XPath.XmlSortOrder.Ascending, System.Xml.XPath.XmlCaseOrder.None, &quot;&quot;, 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(&quot;Titles sorted in Ascending order...&quot;); System.Diagnostics.Debug.WriteLine(&quot;***********************************&quot;);

// Use the iterator to explore the result set that is generated. while (iterator.MoveNext) {  System.Diagnostics.Debug.WriteLine(iterator.Current.Value); }                   </li> <li> 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: <pre class="fixed_text"> 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, &quot;us-en&quot; 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. </li></ol>

back to the top

Test the Sample Code
<ol> <li>Save the changes to the Visual C# .NET project, and then run the project.</li> <li> 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: <pre class="fixed_text">Titles sorted in Ascending order... Advanced XML Developing XML solutions Learn XML Today XML for Gurus </li></ol>

back to the top

<div class="references_section">