XmlElement.GetElementsByTagName 方法

定義

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

多載

GetElementsByTagName(String, String)

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

GetElementsByTagName(String)

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

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

要相符的區域名稱。The local name to match. 星號 (*) 是與所有標記相符的特殊值。The asterisk (*) is a special value that matches all tags.

namespaceURI
String

要相符的命名空間 URI。The namespace URI to match.

傳回

XmlNodeList,包含所有符合節點的清單。An XmlNodeList containing a list of all matching nodes. 如果沒有相符的節點,則清單為空白。The list is empty if there are no matching nodes.

備註

節點會以在XmlElement樹狀結構的預購遍歷中遇到的順序來放置。The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

注意

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

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

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

傳回

XmlNodeList,包含所有符合節點的清單。An XmlNodeList containing a list of all matching nodes. 如果沒有相符的節點,則清單為空白。The list is empty if there are no matching nodes.

範例

下列範例會取得並顯示所有書名。The following example gets and displays all the book titles.

#using <System.Xml.dll>

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

int main()
{
   XmlDocument^ doc = gcnew 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 );
   }
}
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);
     } 
    
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.Load("2books.xml")
                         
     ' Get and display all the 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
        Console.WriteLine(elemList.ItemOf(i).InnerXml)
     next
    
  end sub
end class

此範例會使用2books.xml檔案作為輸入。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>

備註

節點會以在XmlElement樹狀結構的預購遍歷中遇到的順序來放置。The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

注意

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

適用於