XmlNode.SelectSingleNode Metode

Definisi

Memilih yang pertama XmlNode yang cocok dengan ekspresi XPath.

Overload

Nama Deskripsi
SelectSingleNode(String)

Memilih yang pertama XmlNode yang cocok dengan ekspresi XPath.

SelectSingleNode(String, XmlNamespaceManager)

Memilih yang pertama XmlNode yang cocok dengan ekspresi XPath. Setiap awalan yang ditemukan dalam ekspresi XPath diselesaikan menggunakan yang disediakan XmlNamespaceManager.

Contoh

Contoh berikut mengembalikan buku pertama dengan nama penulis yang cocok. XmlNamespaceManager menyelesaikan namespace default dalam ekspresi XPath.

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 the first book written by an author whose last name is Atwood.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
     book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr);

      Console.WriteLine(book.OuterXml);
  }
}
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 resolving namespaces.
      Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:newbooks-schema")

      'Select the book written by an author whose last name is Atwood.
      Dim book As XmlNode 
      Dim root As XmlElement = doc.DocumentElement
      book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr)

      Console.WriteLine(book.OuterXml)

  End Sub
End Class

Contohnya menggunakan file, newbooks.xml, sebagai input.

<?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>

Keterangan

Ekspresi XPath dapat menyertakan namespace layanan. Resolusi namespace didukung menggunakan XmlNamespaceManager. Jika ekspresi XPath menyertakan awalan, pasangan URI awalan dan namespace harus ditambahkan ke XmlNamespaceManager.

Nota

Jika ekspresi XPath tidak menyertakan awalan, diasumsikan bahwa URI namespace adalah namespace kosong. Jika XML Anda menyertakan namespace default, Anda masih harus menambahkan awalan dan namespace URI ke XmlNamespaceManager; jika tidak, Anda tidak akan mendapatkan simpul apa pun yang dipilih. Untuk informasi selengkapnya, lihat Memilih Simpul Menggunakan Navigasi XPath.

SelectSingleNode(String)

Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs

Memilih yang pertama XmlNode yang cocok dengan ekspresi XPath.

public:
 System::Xml::XmlNode ^ SelectSingleNode(System::String ^ xpath);
public System.Xml.XmlNode? SelectSingleNode(string xpath);
public System.Xml.XmlNode SelectSingleNode(string xpath);
member this.SelectSingleNode : string -> System.Xml.XmlNode
Public Function SelectSingleNode (xpath As String) As XmlNode

Parameter

xpath
String

Ekspresi XPath.

Mengembalikan

Yang pertama XmlNode yang cocok dengan kueri XPath atau null jika tidak ada simpul yang cocok yang ditemukan.

Pengecualian

Ekspresi XPath berisi awalan.

Contoh

Contoh berikut mengubah harga buku Jane Austen pertama.

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

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

    XmlNode book;
    XmlNode root = doc.DocumentElement;

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']");

    //Change the price on the book.
    book.LastChild.InnerText="15.95";

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    'Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.Load("booksort.xml")
           
    Dim book as XmlNode
    Dim root as XmlNode = doc.DocumentElement

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']")
 
    'Change the price on the book.
    book.LastChild.InnerText="15.95"

    Console.WriteLine("Display the modified XML document....")
    doc.Save(Console.Out)
    
  end sub
end class

Contohnya menggunakan file, booksort.xml, sebagai 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>

Keterangan

Jika ekspresi XPath memerlukan resolusi namespace, Anda harus menggunakan SelectSingleNode kelebihan beban yang mengambil XmlNamespaceManager sebagai argumennya. XmlNamespaceManager digunakan untuk mengatasi namespace layanan.

Nota

Jika ekspresi XPath tidak menyertakan awalan, diasumsikan bahwa URI namespace adalah namespace kosong. Jika XML Anda menyertakan namespace default, Anda masih harus menggunakan XmlNamespaceManager dan menambahkan awalan dan namespace URI ke dalamnya; jika tidak, Anda tidak akan mendapatkan simpul yang dipilih. Untuk informasi selengkapnya, lihat Memilih Simpul Menggunakan Navigasi XPath.

Nota

Masalah umum saat merumuskan ekspresi XPath adalah cara menyertakan tanda kutip tunggal (') atau tanda kutip ganda (") dalam ekspresi. Jika Anda harus mencari nilai yang menyertakan kuotasi tunggal, Anda harus mengapit string dalam tanda kutip ganda. Jika Anda perlu mencari nilai yang menyertakan tanda kutip ganda, Anda harus mengapit string dalam tanda kutip tunggal.

Misalnya, Anda memiliki XML berikut:

<bookstore>
  <book>
    <title>&apos;Emma&apos;</title>
  </book>
</bookstore>

Kode Visual Basic berikut memilih elemen yang berisi tanda kutip tunggal:

book = root.SelectSingleNode("descendant::book[title=""'Emma'""]")

Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).

Lihat juga

Berlaku untuk

SelectSingleNode(String, XmlNamespaceManager)

Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs
Sumber:
XmlNode.cs

Memilih yang pertama XmlNode yang cocok dengan ekspresi XPath. Setiap awalan yang ditemukan dalam ekspresi XPath diselesaikan menggunakan yang disediakan XmlNamespaceManager.

public:
 System::Xml::XmlNode ^ SelectSingleNode(System::String ^ xpath, System::Xml::XmlNamespaceManager ^ nsmgr);
public System.Xml.XmlNode? SelectSingleNode(string xpath, System.Xml.XmlNamespaceManager nsmgr);
public System.Xml.XmlNode SelectSingleNode(string xpath, System.Xml.XmlNamespaceManager nsmgr);
member this.SelectSingleNode : string * System.Xml.XmlNamespaceManager -> System.Xml.XmlNode
Public Function SelectSingleNode (xpath As String, nsmgr As XmlNamespaceManager) As XmlNode

Parameter

xpath
String

Ekspresi XPath.

nsmgr
XmlNamespaceManager

Yang XmlNamespaceManager digunakan untuk mengatasi namespace layanan untuk awalan dalam ekspresi XPath.

Mengembalikan

Yang pertama XmlNode yang cocok dengan kueri XPath atau null jika tidak ada simpul yang cocok yang ditemukan.

Pengecualian

Ekspresi XPath berisi awalan yang tidak ditentukan dalam XmlNamespaceManager.

Contoh

Contoh berikut memilih buku dengan nilai ISBN yang cocok.

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

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

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Select the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);

      Console.WriteLine(book.OuterXml);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

      Dim doc as XmlDocument = new XmlDocument()
      doc.Load("booksort.xml")

      'Create an XmlNamespaceManager for resolving namespaces.
      Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
      nsmgr.AddNamespace("bk", "urn:samples")

      'Select the book node with the matching attribute value.
      Dim book as XmlNode 
      Dim root as XmlElement = doc.DocumentElement
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr)

      Console.WriteLine(book.OuterXml)

  end sub
end class

Contohnya menggunakan file, booksort.xml, sebagai 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>

Keterangan

Ekspresi XPath dapat menyertakan namespace layanan. Resolusi namespace didukung menggunakan XmlNamespaceManager. Jika ekspresi XPath menyertakan awalan, pasangan URI awalan dan namespace harus ditambahkan ke XmlNamespaceManager.

Nota

Jika ekspresi XPath tidak menyertakan awalan, diasumsikan bahwa URI namespace adalah namespace kosong. Jika XML Anda menyertakan namespace default, Anda masih harus menambahkan awalan dan URI namespace ke XmlNamespaceManager; jika tidak, Anda tidak akan mendapatkan simpul yang dipilih. Untuk informasi selengkapnya, lihat Memilih Simpul Menggunakan Navigasi XPath.

Misalnya, jika Anda memiliki XML berikut:

<bookstore xmlns="http://www.lucernepublishing.com">
 <book>
   <title>Pride And Prejudice</title>
 </book>
</bookstore>

Kode C# berikut memilih simpul buku pertama:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

Nota

Masalah umum saat merumuskan ekspresi XPath adalah cara menyertakan tanda kutip tunggal (') atau tanda kutip ganda (") dalam ekspresi. Jika Anda harus mencari nilai yang menyertakan kuotasi tunggal, Anda harus mengapit string dalam tanda kutip ganda. Jika Anda perlu mencari nilai yang menyertakan tanda kutip ganda, Anda harus mengapit string dalam tanda kutip tunggal.

Misalnya, Anda memiliki XML berikut:

<bookstore xmlns="http://www.lucernepublishing.com">
  <book>
    <title>&apos;Emma&apos;</title>
  </book>
</bookstore>

Kode Visual Basic berikut memilih elemen yang berisi tanda kutip tunggal:

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
book = root.SelectSingleNode("descendant::ab:book[ab:title=""'Emma'""]", nsmgr)

Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).

Lihat juga

Berlaku untuk