XmlDocument.GetElementsByTagName XmlDocument.GetElementsByTagName XmlDocument.GetElementsByTagName XmlDocument.GetElementsByTagName Method

定義

傳回 XmlNodeList,其中包含符合指定名稱之所有子代 (Descendant) 項目清單。Returns an XmlNodeList containing a list of all descendant elements that match the specified name.

多載

GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String)

傳回 XmlNodeList,其中包含符合指定 Name 之所有子代 (Descendant) 項目的清單。Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.

GetElementsByTagName(String, String) GetElementsByTagName(String, String) GetElementsByTagName(String, String) GetElementsByTagName(String, String)

傳回 XmlNodeList,其中包含符合指定之 LocalNameNamespaceURI 的所有子代元素的清單。Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.

GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String)

傳回 XmlNodeList,其中包含符合指定 Name 之所有子代 (Descendant) 項目的清單。Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.

public:
 virtual System::Xml::XmlNodeList ^ GetElementsByTagName(System::String ^ name);
public virtual System.Xml.XmlNodeList GetElementsByTagName (string name);
abstract member GetElementsByTagName : string -> System.Xml.XmlNodeList
override this.GetElementsByTagName : string -> System.Xml.XmlNodeList
Public Overridable Function GetElementsByTagName (name As String) As XmlNodeList

參數

name
String String String String

要相符的限定名稱。The qualified name to match. 它會與符合節點的 Name 屬性比對。It is matched against the Name property of the matching node. 特殊值 "*" 與所有標記相符。The special value "*" matches all tags.

傳回

XmlNodeList,包含所有符合節點的清單。An XmlNodeList containing a list of all matching nodes. 如果沒有節點符合 name,就會傳回空的集合。If no nodes match name, the returned collection will be empty.

範例

下列範例會建立XmlDocument物件, 並GetElementsByTagName使用方法和所產生XmlNodeList的物件來顯示所有書名。The following example creates a XmlDocument object and uses the GetElementsByTagName method and the resulting XmlNodeList object to display all the book titles.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "books.xml" );
   
   //Display all the book titles.
   XmlNodeList^ elemList = doc->GetElementsByTagName( "title" );
   for ( int i = 0; i < elemList->Count; i++ )
   {
      Console::WriteLine( elemList[ i ]->InnerXml );
   }
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.Load("books.xml");

    //Display all the book titles.
    XmlNodeList elemList = doc.GetElementsByTagName("title");
    for (int i=0; i < elemList.Count; i++)
    {   
      Console.WriteLine(elemList[i].InnerXml);
    }  

  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.Load("books.xml")
        
        'Display all the book titles.
        Dim elemList As XmlNodeList = doc.GetElementsByTagName("title")
        Dim i As Integer
        For i = 0 To elemList.Count - 1
            Console.WriteLine(elemList(i).InnerXml)
        Next i
    End Sub
End Class

該範例使用 books.xml 檔案做為輸入。The example uses the books.xml file as input.

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

備註

這些節點會以在檔中遇到的順序放置。The nodes are placed in the order in which they would be encountered in the document.

注意

建議您使用XmlNode.SelectNodes GetElementsByTagNameXmlNode.SelectSingleNode方法, 而不要使用方法。It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

GetElementsByTagName(String, String) GetElementsByTagName(String, String) GetElementsByTagName(String, String) GetElementsByTagName(String, String)

傳回 XmlNodeList,其中包含符合指定之 LocalNameNamespaceURI 的所有子代元素的清單。Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.

public:
 virtual System::Xml::XmlNodeList ^ GetElementsByTagName(System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlNodeList GetElementsByTagName (string localName, string namespaceURI);
abstract member GetElementsByTagName : string * string -> System.Xml.XmlNodeList
override this.GetElementsByTagName : string * string -> System.Xml.XmlNodeList
Public Overridable Function GetElementsByTagName (localName As String, namespaceURI As String) As XmlNodeList

參數

localName
String String String String

要相符的 LocalName。The LocalName to match. 特殊值 "*" 與所有標記相符。The special value "*" matches all tags.

namespaceURI
String String String String

要比對的 NamespaceURI。NamespaceURI to match.

傳回

XmlNodeList,包含所有符合節點的清單。An XmlNodeList containing a list of all matching nodes. 如果沒有節點符合指定的 localNamenamespaceURI,就會傳回空的集合。If no nodes match the specified localName and namespaceURI, the returned collection will be empty.

備註

節點會以在檔樹狀結構中遇到的順序放置。The nodes are placed in the order in which they would be encountered in the document tree.

注意

建議您使用XmlNode.SelectNodes GetElementsByTagNameXmlNode.SelectSingleNode方法, 而不要使用方法。It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

適用於