次の方法で共有


インデックスによる順序付けられたノードの取得

W3C (World Wide Web Consortium) の XML ドキュメント オブジェクト モデル (DOM) では、XmlNamedNodeMap によって処理される順序付けられていないノード セットとは対照的に、順序付けられたノードのリストを処理する機能を持った NodeList も定義しています。 Microsoft .NET Framework の NodeList は XmlNodeList と呼ばれています。 XmlNodeList を返すメソッドとプロパティは次のとおりです。

  • XmlNode.ChildNodes

  • XmlDocument.GetElementsByTagName

  • XmlElement.GetElementsByTagName

  • XmlNode.SelectNodes

XmlNodeList には Count プロパティがあり、次のコード サンプルに示すように、ループを記述して XmlNodeList のノードを反復処理するために使用できます。

 Dim doc as XmlDocument = new XmlDocument()
    doc.Load("books.xml")
                         
     ' Retrieve all book titles.
     Dim root as XmlElement = doc.DocumentElement
     Dim elemList as XmlNodeList = root.GetElementsByTagName("title")
     Dim i as integer
     for i=0  to elemList.Count-1
         ' Display all book titles in the Node List.
         Console.WriteLine(elemList.ItemOf(i).InnerXml)
     next
    
     XmlDocument doc = new XmlDocument();
     doc.Load("books.xml");
     // Retrieve all book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     for (int i=0; i < elemList.Count; i++)
     {   
        // Display all book titles in the Node List.
        Console.WriteLine(elemList[i].InnerXml);
     } 

Count プロパティの他に、XmlNodeList 内のノード コレクションに対して foreach スタイルの反復処理を実行する GetEnumerator メソッドがあります。 foreach ステートメントの使用方法を次のコード サンプルに示します。

Dim doc As New XmlDocument()
doc.Load("books.xml")

' Get book titles.
Dim root As XmlElement = doc.DocumentElement
Dim elemList As XmlNodeList = root.GetElementsByTagName("title")
Dim ienum As IEnumerator = elemList.GetEnumerator()
' Loop over the XmlNodeList using the enumerator ienum        
While ienum.MoveNext()
    ' Display the book title.
    Dim title As XmlNode = CType(ienum.Current, XmlNode)
    Console.WriteLine(title.InnerText)
End While
{
     XmlDocument doc = new XmlDocument();
     doc.Load("books.xml");
                         
     // Get book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     IEnumerator ienum = elemList.GetEnumerator();  
     // Loop over the XmlNodeList using the enumerator ienum        
     while (ienum.MoveNext())
     {
          // Display the book title.
           XmlNode title = (XmlNode) ienum.Current;
           Console.WriteLine(title.InnerText);
     }
  }

XmlNodeList で利用可能なメソッドとプロパティの詳細については、「XmlNodeList メンバー」を参照してください。

参照

概念

XML ドキュメント オブジェクト モデル (DOM)