Gets the XmlElement with the specified ID.
[Visual Basic]
Public Overridable Function GetElementById( _
ByVal elementId As String _
) As XmlElement
[C#]
public virtual XmlElement GetElementById(
string elementId
);
[C++]
public: virtual XmlElement* GetElementById(
String* elementId
);
[JScript]
public function GetElementById(
elementId : String
) : XmlElement;
Parameters
- elementId
- The attribute ID to match.
Return Value
The XmlElement with the matching ID or a null reference (Nothing in Visual Basic) if no matching element is found.
Remarks
If the document has multiple elements with the matching ID, this method returns the first matching element in the document.
Note The DOM implementation must have information which defines which attributes are of type ID. 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. Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementations where it is unknown whether the attributes are of type ID are expected to return a null reference (Nothing in Visual Basic).
Example
[Visual Basic, C#, C++] The following example uses the GetElementById method.
[Visual Basic]
Option Explicit
Option Strict
Imports System
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 'Main
End Class 'Sample
[C#]
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 );
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
int main()
{
XmlDocument* doc = new XmlDocument();
doc->Load(S"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(S"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(S"A222");
Console::WriteLine(elem->OuterXml);
}
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='A111'/>
<Person SSN='A222' Name='Tom'/>
<Customer id='A111'/>
<Customer id='A222334444'/>
<Team members='A222334444 A333445555'/>
</root>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
XmlDocument Class | XmlDocument Members | System.Xml Namespace