인덱스를 사용한 정렬된 노드 검색

업데이트: November 2007

W3C(World Wide Web 컨소시엄) DOM(문서 개체 모델)에서는 XmlNamedNodeMap으로 처리되는 정렬되지 않은 집합과 반대되는 정렬된 노드 목록을 처리할 수 있는 NodeList에 대해서도 설명합니다. Microsoft .NET Framework에서는 NodeList를 XmlNodeList라고 합니다. XmlNodeList를 반환하는 메서드와 속성은 다음과 같습니다.

  • XmlNode.ChildNodes

  • XmlDocument.GetElementsByTagName

  • XmlElement.GetElementsByTagName

  • XmlNode.SelectNodes

XmlNodeList에는 다음 코드 샘플과 같이 XmlNodeList의 노드를 반복하는 루프를 작성하는 데 사용할 수 있는 Count 속성이 있습니다.

 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(문서 개체 모델)