System.Xml.XmlNodeList

Overview

The XmlNodeList class provides an ordered collection of nodes from an XmlDocument. It implements IEnumerable and allows iteration over the nodes it contains.

Syntax

public abstract class XmlNodeList : IEnumerable, IEnumerable<XmlNode>

Properties

NameTypeDescription
CountintGets the number of nodes in the list.
ItemXmlNodeGets the node at the specified index.
this[int index]XmlNodeIndexer to retrieve a node by its position.

Methods

NameSignatureDescription
GetEnumeratorIEnumerator GetEnumerator()Returns an enumerator that iterates through the collection.
CopyTovoid CopyTo(Array array, int index)Copies the entire collection to a compatible one-dimensional array, starting at the specified index.

Examples

The following example loads an XML document and retrieves a list of all <book> elements.

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        var xml = @"<catalog>
                        <book id='b1'>
                            <title>XML Developer's Guide</title>
                        </book>
                        <book id='b2'>
                            <title>Midnight Rain</title>
                        </book>
                    </catalog>";

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

        XmlNodeList books = doc.GetElementsByTagName("book");
        Console.WriteLine($"Found {books.Count} books:");

        foreach (XmlNode book in books)
        {
            Console.WriteLine($"- {book.Attributes[""id""].Value}: {book[""title""].InnerText}");
        }
    }
}

Remarks