XmlDocument.GetElementById(String) XmlDocument.GetElementById(String) XmlDocument.GetElementById(String) XmlDocument.GetElementById(String) Method

Definición

Obtiene el XmlElement con el identificador especificado.Gets the XmlElement with the specified ID.

public:
 virtual System::Xml::XmlElement ^ GetElementById(System::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

Parámetros

elementId
String String String String

Identificador de atributo con el que debe haber una coincidencia.The attribute ID to match.

Devoluciones

XmlElement con el identificador coincidente o null si no se encuentra un elemento coincidente.The XmlElement with the matching ID or null if no matching element is found.

Ejemplos

El siguiente ejemplo utiliza el método GetElementById.The following example uses the GetElementById method.

#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

En el ejemplo se usa el ids.xmlarchivo,, como entrada.The example uses the file, ids.xml, as 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>  

Comentarios

Si el documento tiene varios elementos con el identificador coincidente, este método devuelve el primer elemento coincidente del documento.If the document has multiple elements with the matching ID, this method returns the first matching element in the document.

Nota

La implementación de DOM debe tener información que defina qué atributos son de tipo ID.The DOM implementation must have information which defines which attributes are of type ID. Aunque los atributos de tipo ID pueden definirse en esquemas XSD o en DTD, esta versión del producto solo es compatible con los definidos en DTD.Although attributes of type ID can be defined in either XSD schemas or DTDs, this version of the product only supports those defined in DTDs. Los atributos con el nombre "ID" no son de tipo ID a menos que estén definidos en la DTD.Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementaciones en las que se desconoce si se espera que los atributos de tipo ID nulldevuelvan.Implementations where it is unknown whether the attributes are of type ID are expected to return null.

Se aplica a