XmlDocument.GetElementById(String) Metodo

Definizione

Ottiene l'oggetto XmlElement con l'ID specificato.

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

Parametri

elementId
String

ID dell'attributo di cui verificare la corrispondenza.

Restituisce

XmlElement

Oggetto XmlElement con l'ID corrispondente o null se non vengono trovati elementi corrispondenti.

Esempio

Nell'esempio seguente viene usato il metodo 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

Nell'esempio viene usato il file, , ids.xmlcome input.

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

Commenti

Se il documento contiene più elementi con l'ID corrispondente, questo metodo restituisce il primo elemento corrispondente nel documento.

Nota

L'implementazione DOM deve avere informazioni che definiscono quali attributi sono di tipo ID. Anche se gli attributi di ID di tipo possono essere definiti in schemi XSD o DTD, questa versione del prodotto supporta solo quelli definiti in DTD. Gli attributi con il nome "ID" non sono di tipo ID, a meno che non sia stato definito in DTD. Le implementazioni in cui è sconosciuto se gli attributi sono di tipo ID devono restituire null.

Si applica a