MS Microsoft Docs

Item Property

Summary

Gets the XmlNode at the specified index in the underlying node collection.

Syntax

C#
VB
F#
public XmlNode this[int i] { get; }
Public ReadOnly Default Property Item(i As Integer) As XmlNode
member this.Item : int -> XmlNode

Parameters

ParameterDescription
iZero‑based index of the node to retrieve.

Returns

The XmlNode located at the specified index.

Exceptions

Example

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        string xml = @"<books>
                           <book id='1'><title>C# in Depth</title></book>
                           <book id='2'><title>Pro .NET</title></book>
                       </books>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        using (XmlNodeReader reader = new XmlNodeReader(doc))
        {
            // Move to the first element
            reader.Read();

            // Access the first child node via the Item property
            XmlNode firstBook = reader[0];
            Console.WriteLine($"First book title: {firstBook[\"title\"].InnerText}");
        }
    }
}

See Also