Microsoft KB Archive/317084

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

Article ID: 317084

Article Last Modified on 9/22/2003

-

APPLIES TO


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

-



This article was previously published under Q317084



For a Microsoft Visual C# .NET version of this article, see 318542.

In This Task
SUMMARY
 * Create the Sample XML Document
 * Create the Sample Visual Basic .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

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 Basic .NET Application
<ol> <li>Follow these steps to create a new Visual Basic .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 Basic Projects under Project Types, and then click Windows Application under Templates.</li></ol> </li> <li>Drag a Button control from the toolbox to Form1.vb.</li> <li> Add the following code in the Click event procedure of the Button control: 'Construct the XPathDocument by specifying the path to Books.xml. Dim xmldoc As New System.Xml.XPath.XPathDocument(&quot;c:\books.xml&quot;)

'Create XPathNavigator. Dim nav As System.Xml.XPath.XPathNavigator = 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. Dim expr As System.Xml.XPath.XPathExpression = 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;, 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. Dim iterator As System.Xml.XPath.XPathNodeIterator = 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. Do While iterator.MoveNext System.Diagnostics.Debug.WriteLine(iterator.Current.Value) Loop </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.

The following is a description of the parameters in the overload of the AddSort method that are used in this sample: MustOverride Overloads Public Sub AddSort( _  ByVal expr As Object, _   ByVal order As XmlSortOrder, _   ByVal caseOrder As XmlCaseOrder, _   ByVal lang As String, _   ByVal dataType As XmlDataType _ ) <pre class="fixed_text">Parameters  Description

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, which 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 Basic .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

Additional query words: execute XPath queries

Keywords: kbhowtomaster KB317084

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.