XmlNode.SelectSingleNode 메서드

정의

XPath 식과 일치하는 첫 번째 XmlNode를 선택해야 합니다.

오버로드

SelectSingleNode(String)

XPath 식과 일치하는 첫 번째 XmlNode를 선택해야 합니다.

SelectSingleNode(String, XmlNamespaceManager)

XPath 식과 일치하는 첫 번째 XmlNode를 선택해야 합니다. XPath 식에 있는 접두사는 제공된 XmlNamespaceManager를 사용해 확인합니다.

예제

다음 예제에서는 일치하는 작성자 이름을 가진 첫 번째 책을 반환합니다. XmlNamespaceManager는 XPath 식에서 기본 네임스페이스를 확인합니다.

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

이 예제에서는 파일 를 newbooks.xml입력으로 사용합니다.

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

설명

XPath 식에 네임스페이스를 포함할 수 있습니다. 네임스페이스는 XmlNamespaceManager를 사용하여 확인할 수 있습니다. XPath 식에 접두사를 포함하는 경우 접두사 및 네임스페이스 URI 쌍을 에 XmlNamespaceManager추가해야 합니다.

참고

XPath 식에 접두사를 포함하지 않으면 네임스페이스 URI가 빈 네임스페이스라고 가정합니다. XML에 기본 네임스페이스가 포함된 경우 접두사 및 네임스페이스 URI를 XmlNamespaceManager에 추가해야 합니다. 그렇지 않으면 노드가 선택되지 않습니다. 자세한 내용은 XPath 탐색을 사용하여 노드 선택을 참조하세요.

SelectSingleNode(String)

Source:
XmlNode.cs
Source:
XmlNode.cs
Source:
XmlNode.cs

XPath 식과 일치하는 첫 번째 XmlNode를 선택해야 합니다.

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

매개 변수

xpath
String

XPath 식입니다. XPath 예제를 참조하세요.

반환

XPath 쿼리와 일치하는 첫 번째 XmlNode이거나, 일치하는 노드가 없는 경우에는 null입니다.

예외

XPath 식에 접두사가 포함되어 있는 경우

예제

다음 예제에서는 첫 번째 Jane Austen 책의 가격을 변경합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}
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

이 예제에서는 파일 를 booksort.xml입력으로 사용합니다.


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

설명

XPath 식에 네임스페이스 확인이 필요한 경우 를 인수로 사용하는 오버로드를 XmlNamespaceManager 사용해야 SelectSingleNode 합니다. 는 XmlNamespaceManager 네임스페이스를 확인하는 데 사용됩니다.

참고

XPath 식에 접두사를 포함하지 않으면 네임스페이스 URI가 빈 네임스페이스라고 가정합니다. XML에 기본 네임스페이스가 포함되어 있는 경우에도 를 사용하고 XmlNamespaceManager 접두사 및 네임스페이스 URI를 추가해야 합니다. 그렇지 않으면 선택한 노드가 표시되지 않습니다. 자세한 내용은 XPath 탐색을 사용하여 노드 선택을 참조하세요.

참고

XPath 식을 작성할 때 일반적인 문제는 식에 작은따옴표(') 또는 큰따옴표(")를 포함하는 방법입니다. 작은따옴표를 포함하는 값을 검색해야 하는 경우 문자열을 큰따옴표로 묶어야 합니다. 큰따옴표를 포함하는 값을 검색해야 하는 경우 문자열을 작은따옴표로 묶어야 합니다.

예를 들어 다음과 같은 XML이 있다고 가정합니다.

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

다음 Visual Basic 코드는 작은따옴표를 포함하는 요소를 선택합니다.

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

이 메서드는 DOM(문서 개체 모델)에 대한 Microsoft 확장입니다.

추가 정보

적용 대상

SelectSingleNode(String, XmlNamespaceManager)

Source:
XmlNode.cs
Source:
XmlNode.cs
Source:
XmlNode.cs

XPath 식과 일치하는 첫 번째 XmlNode를 선택해야 합니다. XPath 식에 있는 접두사는 제공된 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

매개 변수

xpath
String

XPath 식입니다. XPath 예제를 참조하세요.

nsmgr
XmlNamespaceManager

XPath 식의 접두사에 대한 네임스페이스를 확인하기 위해 사용할 XmlNamespaceManager입니다.

반환

XPath 쿼리와 일치하는 첫 번째 XmlNode이거나, 일치하는 노드가 없는 경우에는 null입니다.

예외

XPath 식에 XmlNamespaceManager에서 정의되지 않은 접두사가 포함되어 있는 경우

예제

다음 예제에서는 일치하는 ISBN 값이 있는 책을 선택합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "booksort.xml" );
   
   //Create an XmlNamespaceManager for resolving namespaces.
   XmlNamespaceManager^ nsmgr = gcnew 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->Item[@bk:ISBN='1-861001-57-6']", nsmgr );
   Console::WriteLine( book->OuterXml );
}
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

이 예제에서는 파일 를 booksort.xml입력으로 사용합니다.


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

설명

XPath 식에 네임스페이스를 포함할 수 있습니다. 네임스페이스는 XmlNamespaceManager를 사용하여 확인할 수 있습니다. XPath 식에 접두사를 포함하는 경우 접두사 및 네임스페이스 URI 쌍을 에 XmlNamespaceManager추가해야 합니다.

참고

XPath 식에 접두사를 포함하지 않으면 네임스페이스 URI가 빈 네임스페이스라고 가정합니다. XML에 기본 네임스페이스가 포함된 경우 접두사 및 네임스페이스 URI를 XmlNamespaceManager에 추가해야 합니다. 그렇지 않으면 노드가 선택되지 않습니다. 자세한 내용은 XPath 탐색을 사용하여 노드 선택을 참조하세요.

예를 들어 다음 XML이 있는 경우:

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

다음 C# 코드는 첫 번째 책 노드를 선택합니다.

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

참고

XPath 식을 작성할 때 일반적인 문제는 식에 작은따옴표(') 또는 큰따옴표(")를 포함하는 방법입니다. 작은따옴표를 포함하는 값을 검색해야 하는 경우 문자열을 큰따옴표로 묶어야 합니다. 큰따옴표를 포함하는 값을 검색해야 하는 경우 문자열을 작은따옴표로 묶어야 합니다.

예를 들어 다음과 같은 XML이 있다고 가정합니다.

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

다음 Visual Basic 코드는 작은따옴표를 포함하는 요소를 선택합니다.

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)

이 메서드는 DOM(문서 개체 모델)에 대한 Microsoft 확장입니다.

추가 정보

적용 대상