This documentation is archived and is not being maintained.

XmlNodeList.Item Method

Retrieves a node at the given index.

[Visual Basic]
Public MustOverride Function Item( _
   ByVal index As Integer _
) As XmlNode
[C#]
public abstract XmlNode Item(
 int index
);
[C++]
public: virtual XmlNode* Item(
 int index
) = 0;
[JScript]
public abstract function Item(
   index : int
) : XmlNode;

Parameters

index
Zero-based index into the list of nodes.

Return Value

The XmlNode in the collection. If index is greater than or equal to the number of nodes in the list, this returns a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following example gets and displays the second node in the XmlNodeList.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<items>" & _
                "  <item>First item</item>" & _
                "  <item>Second item</item>" & _
                "</items>")
                         
     'Get and display the last item node.
     Dim root as XmlElement = doc.DocumentElement
     Dim nodeList as XmlNodeList = root.GetElementsByTagName("item")
     Console.WriteLine(nodeList.Item(1).InnerXml)
    
  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {
  
     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<items>" +
                 "  <item>First item</item>" +
                 "  <item>Second item</item>" +
                 "</items>");
                         
     //Get and display the last item node.
     XmlElement root = doc.DocumentElement;
     XmlNodeList nodeList = root.GetElementsByTagName("item");
     Console.WriteLine(nodeList.Item(1).InnerXml);     
    
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
     XmlDocument* doc = new XmlDocument();
     doc->LoadXml(S"<items>" 
                  S"  <item>First item</item>" 
                  S"  <item>Second item</item>" 
                  S"</items>");
                         
     //Get and display the last item node.
     XmlElement* root = doc->DocumentElement;
     XmlNodeList* nodeList = root->GetElementsByTagName(S"item");
     Console::WriteLine(nodeList->Item(1)->InnerXml);     
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter 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, .NET Compact Framework

See Also

XmlNodeList Class | XmlNodeList Members | System.Xml Namespace

Show: