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されることが予想されます。

適用対象