Export (0) Print
Expand All

XPathNavigator.Select Method

Selects a node set using the specified XPath expression.

Overload List

Selects a node set using the specified XPath expression.

[Visual Basic] Overloads Public Overridable Function Select(String) As XPathNodeIterator
[C#] public virtual XPathNodeIterator Select(string);
[C++] public: virtual XPathNodeIterator* Select(String*);
[JScript] public function Select(String) : XPathNodeIterator;

Selects a node set using the specified XPathExpression.

[Visual Basic] Overloads Public Overridable Function Select(XPathExpression) As XPathNodeIterator
[C#] public virtual XPathNodeIterator Select(XPathExpression);
[C++] public: virtual XPathNodeIterator* Select(XPathExpression*);
[JScript] public function Select(XPathExpression) : XPathNodeIterator;

Example

[Visual Basic, C#, C++] The following example selects all books by Jane Austen and sorts them by title.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Select. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

     Dim doc as XPathDocument = new XPathDocument("booksort.xml")
     Dim nav as XPathNavigator = doc.CreateNavigator()

     'Select all books by Jane Austen.
     Dim expr as XPathExpression 
     expr = nav.Compile("descendant::book[author/last-name='Austen']")

     'Sort the selected books by title.
     expr.AddSort("title", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text)

     'Display the selection.
     Dim iterator as XPathNodeIterator = nav.Select(expr)
     while (iterator.MoveNext())
        Dim nav2 as XPathNavigator = iterator.Current.Clone()
        nav2.MoveToFirstChild()
        Console.WriteLine("Book title:  {0}", nav2.Value)
     end while
  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Sample
{
  public static void Main()
  {

      XPathDocument doc = new XPathDocument("booksort.xml");
      XPathNavigator nav = doc.CreateNavigator();

      //Select all books by Jane Austen.
      XPathExpression expr; 
      expr = nav.Compile("descendant::book[author/last-name='Austen']");

      //Sort the selected books by title.
      expr.AddSort("title", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);

      //Display the selection.
      XPathNodeIterator iterator = nav.Select(expr);
      while (iterator.MoveNext()){
        XPathNavigator nav2 = iterator.Current.Clone();
        nav2.MoveToFirstChild();
        Console.WriteLine("Book title:  {0}", nav2.Value);
      }
   }

}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;

int main()
{

   XPathDocument* doc = new XPathDocument(S"booksort.xml");
   XPathNavigator* nav = doc->CreateNavigator();

   //Select all books by Jane Austen.
   XPathExpression* expr; 
   expr = nav->Compile(S"descendant::book[author/last-name='Austen']");

   //Sort the selected books by title.
   expr->AddSort(S"title", XmlSortOrder::Ascending, XmlCaseOrder::None, S"", XmlDataType::Text);

   //Display the selection.
   XPathNodeIterator* iterator = nav->Select(expr);
   while (iterator->MoveNext()){
      XPathNavigator* nav2 = iterator->Current->Clone();
      nav2->MoveToFirstChild();
      Console::WriteLine(S"Book title:  {0}", nav2->Value);
   }
}

[Visual Basic, C#, C++] The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace

Show:
© 2014 Microsoft