System.Xml.XmlNodeList Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Represents an ordered collection of nodes.
C# Syntax:
public abstract class XmlNodeList : IEnumerable
Remarks
The XmlNodeList is "live": changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the XmlNodeList properties and methods.
See also:
System.Xml Namespace

System.Xml.XmlNodeList Member List:

Public Properties
Count Read-only

Gets the number of nodes in the XmlNodeList.
ItemOf Read-only

Retrieves a node at the given index.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator Provides a simple "foreach" style iteration over the collection of nodes in the XmlNodeList.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Item Retrieves a node at the given index.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlNodeList Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected XmlNodeList();

Return to top


Property: Count (read-only)
Summary
Gets the number of nodes in the XmlNodeList.
C# Syntax:
public abstract int Count {get;}
Example
The following example creates a XmlDocument object and uses the XmlDocument.GetElementsByTagName method and the resulting XmlNodeList to display all the book titles.


using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.Load("2books.xml");
                         
     //Get and display all the book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     for (int i=0; i < elemList.Count; i++)
     {   
        Console.WriteLine(elemList[i].InnerXml);
     } 
    
  }
}

    
The example uses the file, 2books.xml, as input.
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

    

Return to top


Property: ItemOf (read-only)
Summary
Retrieves a node at the given index.
C# Syntax:
No member name public virtual XmlNode this[int i] {get;}
Parameters:

i

Zero-based index into the list of nodes.

Example
The following example creates a XmlDocument object and uses the XmlDocument.GetElementsByTagName method and the resulting XmlNodeList to display all the book titles.


using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.Load("2books.xml");
                         
     //Get and display all the book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     for (int i=0; i < elemList.Count; i++)
     {   
        Console.WriteLine(elemList[i].InnerXml);
     } 
    
  }
}

    
The example uses the file, 2books.xml, as input.
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlNodeList();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Summary
Provides a simple "foreach" style iteration over the collection of nodes in the XmlNodeList.
C# Syntax:
public abstract IEnumerator GetEnumerator();
Return Value:
An IEnumerator.
Implements:
IEnumerable.GetEnumerator
Example
The following example displays all the book titles.
using System;
using System.IO;
using System.Xml;
using System.Collections;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.Load("2books.xml");
                         
     //Get and display all the book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     IEnumerator ienum = elemList.GetEnumerator();          
     while (ienum.MoveNext())
     {   
       XmlNode title = (XmlNode) ienum.Current;
       Console.WriteLine(title.InnerText);
     }    
    
  }
}

    
The example uses the file, 2books.xml, as input.
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

    

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Item(
   int index
)
Summary
Retrieves a node at the given index.
C# Syntax:
public abstract XmlNode Item(
   int index
);
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 null.
Example
The following example gets and displays the second node in the XmlNodeList.
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);     
    
  }
}

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.