XmlDocument.GetElementById(String) 메서드

정의

지정된 ID의 XmlElement를 가져옵니다.

public:
 virtual System::Xml::XmlElement ^ GetElementById(System::String ^ elementId);
public virtual System.Xml.XmlElement? GetElementById (string elementId);
public virtual System.Xml.XmlElement GetElementById (string elementId);
abstract member GetElementById : string -> System.Xml.XmlElement
override this.GetElementById : string -> System.Xml.XmlElement
Public Overridable Function GetElementById (elementId As String) As XmlElement

매개 변수

elementId
String

일치시킬 특성 ID입니다.

반환

XmlElement

일치하는 ID가 있으면 XmlElement, 일치하는 요소가 없을 경우에는 null입니다.

예제

다음 예제에서는 GetElementById 메서드를 사용합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "ids.xml" );
   
   //Get the first element with an attribute of type ID and value of A111.
   //This displays the node <Person SSN="A111" Name="Fred"/>.
   XmlElement^ elem = doc->GetElementById( "A111" );
   Console::WriteLine( elem->OuterXml );
   
   //Get the first element with an attribute of type ID and value of A222.
   //This displays the node <Person SSN="A222" Name="Tom"/>.
   elem = doc->GetElementById( "A222" );
   Console::WriteLine( elem->OuterXml );
}


using System;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.Load("ids.xml");

    //Get the first element with an attribute of type ID and value of A111.
    //This displays the node <Person SSN="A111" Name="Fred"/>.
    XmlElement elem = doc.GetElementById("A111");
    Console.WriteLine( elem.OuterXml );

    //Get the first element with an attribute of type ID and value of A222.
    //This displays the node <Person SSN="A222" Name="Tom"/>.
    elem = doc.GetElementById("A222");
    Console.WriteLine( elem.OuterXml );
  }
}
Option Explicit
Option Strict

Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.Load("ids.xml")
        
        'Get the first element with an attribute of type ID and value of A111.
        'This displays the node <Person SSN="A111" Name="Fred"/>.
        Dim elem As XmlElement = doc.GetElementById("A111")
        Console.WriteLine(elem.OuterXml)
        
        'Get the first element with an attribute of type ID and value of A222.
        'This displays the node <Person SSN="A222" Name="Tom"/>.
        elem = doc.GetElementById("A222")
        Console.WriteLine(elem.OuterXml)
    End Sub
End Class

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

<!DOCTYPE root [  
  <!ELEMENT root ANY>   
  <!ELEMENT Person ANY>   
  <!ELEMENT Customer EMPTY>  
  <!ELEMENT Team EMPTY>  
  <!ATTLIST Person SSN ID #REQUIRED>  
  <!ATTLIST Customer id IDREF #REQUIRED >  
  <!ATTLIST Team members IDREFS #REQUIRED>]>  
<root>  
  <Person SSN='A111' Name='Fred'/>  
  <Person SSN='A222' Name='Tom'/>  
  <Customer id='A222334444'/>  
  <Team members='A222334444 A333445555'/>  
</root>  

설명

문서에 일치하는 ID를 가진 여러 요소가 있는 경우 이 메서드는 문서의 첫 번째 일치 요소를 반환합니다.

참고

DOM 구현에는 형식 ID의 특성을 정의하는 정보가 있어야 합니다. 형식 ID의 특성은 XSD 스키마 또는 DTD에서 정의할 수 있지만 이 버전의 제품은 DTD에 정의된 특성만 지원합니다. 이름이 "ID"인 특성은 DTD에 정의되지 않는 한 형식 ID가 아닙니다. 특성이 형식 ID인지 여부를 알 수 없는 구현은 반환 null해야 합니다.

적용 대상