.NET Framework Class Library
XmlNode..::.SelectNodes Method

Selects a list of nodes matching the XPath expression.

Overload List

  NameDescription
Public methodSupported by the .NET Compact FrameworkSupported by the XNA FrameworkSelectNodes(String)Selects a list of nodes matching the XPath expression.
Public methodSupported by the .NET Compact FrameworkSupported by the XNA FrameworkSelectNodes(String, XmlNamespaceManager)Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.
Top
Remarks

XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.

NoteNote:

If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected.

For more information, see Select Nodes Using XPath Navigation.

Examples

The following example displays all book titles. The XmlNamespaceManager resolves the default namespace in the XPath expression.

Visual Basic
Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

  Public Shared Sub Main()

      Dim doc As XmlDocument = New XmlDocument()
      doc.Load("newbooks.xml")

      ' Create an XmlNamespaceManager for to resolve the default namespace.
      Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:newbooks-schema")

      ' Select and display all book titles.
      Dim nodeList As XmlNodeList 
      Dim root As XmlElement = doc.DocumentElement
      nodeList = root.SelectNodes("/bk:bookstore/bk:book/bk:title", nsmgr)
      Dim title as XmlNode
      For Each title In nodeList
        Console.WriteLine(title.InnerXml)
      Next

  End Sub
End Class
C#
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("newbooks.xml");

      // Create an XmlNamespaceManager to resolve the default namespace.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:newbooks-schema");

      // Select and display all book titles.
      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;
      nodeList = root.SelectNodes("/bk:bookstore/bk:book/bk:title", nsmgr);
      foreach (XmlNode title in nodeList) {
        Console.WriteLine(title.InnerXml);
      }

   }

}
Visual C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( L"newbooks.xml" );

   // Create an XmlNamespaceManager to resolve the default namespace.
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( doc->NameTable );
   nsmgr->AddNamespace( L"bk", L"urn:newbooks-schema" );

   // Select and display all book titles.
   XmlNodeList^ nodeList;
   XmlElement^ root = doc->DocumentElement;
   nodeList = root->SelectNodes( L"/bk:bookstore/bk:book/bk:title", nsmgr );
   System::Collections::IEnumerator^ myEnum = nodeList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      XmlNode^ title = safe_cast<XmlNode^>(myEnum->Current);
      Console::WriteLine( title->InnerXml );
   }

   return 1;
}

CPP_OLD
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{

    XmlDocument* doc = new XmlDocument();
    doc->Load(S"newbooks.xml");

    // Create an XmlNamespaceManager to resolve the default namespace.
    XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc->NameTable);
    nsmgr->AddNamespace(S"bk", S"urn:newbooks-schema");

    // Select and display all book titles.
    XmlNodeList* nodeList;
    XmlElement* root = doc->DocumentElement;
    nodeList = root->SelectNodes(S"/bk:bookstore/bk:book/bk:title", nsmgr);
    System::Collections::IEnumerator* myEnum = nodeList->GetEnumerator();
    while (myEnum->MoveNext())
    {
        XmlNode* title = __try_cast<XmlNode*>(myEnum->Current);
    Console::WriteLine(title->InnerXml);
    }
    return 1;
}

The example uses the file, newbooks.xml, as input.

None
<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>
    <author>
      <first-name>Barbara</first-name>
      <last-name>Kingsolver</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>
See Also

Reference

Tags :


Page view tracker