XmlDocument.GetElementById(String) Yöntem

Tanım

XmlElement Belirtilen kimlikle alır.

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

Parametreler

elementId
String

Eşleşecek öznitelik kimliği.

Döndürülenler

XmlElement eşleşen kimlikle veya null eşleşen öğe bulunamazsa.

Örnekler

Aşağıdaki örnek GetElementById yöntemini kullanır.


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

Örnekte giriş ids.xmlolarak dosyası kullanılır.

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

Açıklamalar

Belgede eşleşen kimlikle birden çok öğe varsa, bu yöntem belgedeki ilk eşleşen öğeyi döndürür.

Note

DOM uygulaması, hangi özniteliklerin kimlik türünde olduğunu tanımlayan bilgilere sahip olmalıdır. Kimlik türündeki öznitelikler XSD şemalarında veya DTD'lerde tanımlanabilir ancak ürünün bu sürümü yalnızca DTD'lerde tanımlananları destekler. "ID" adlı öznitelikler, DTD'de tanımlanmadığı sürece id türünde değildir. Özniteliklerin kimlik türünde olup olmadığı bilinmediği uygulamalar döndürülmesi nullbeklenir.

Şunlara uygulanır