HOW TO: Use the AddSort Method of the XPathExpression Object in Visual Basic .NET (317084)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .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 Q317084 For a Microsoft Visual C# .NET version of this article, see 318542.
In This TaskSUMMARY
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:
<?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>
- Save the document as Books.xml in the root folder of your hard disk.
back to the top
Create the Sample Visual Basic .NET Application- Follow these steps to create a new Visual Basic .NET Windows Application project:
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
- Drag a Button control from the toolbox to Form1.vb.
- 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("c:\books.xml")
'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("/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, "", 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("Titles Sorted in Ascending Order")
System.Diagnostics.Debug.WriteLine("********************************")
'Use the iterator to explore the result set that is generated.
Do While iterator.MoveNext
System.Diagnostics.Debug.WriteLine(iterator.Current.Value)
Loop
- 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 _
)
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, "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- Save the changes to the Visual Basic .NET project, and then run the project.
- 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
Modification Type: | Major | Last Reviewed: | 9/22/2003 |
---|
Keywords: | kbHOWTOmaster KB317084 kbAudDeveloper |
---|
|