XmlElement.GetElementsByTagName 方法

定義

傳回 XmlNodeList,其中包含符合指定名稱之所有子代 (Descendant) 項目清單。

多載

GetElementsByTagName(String, String)

傳回 XmlNodeList,其中包含符合指定之 LocalNameNamespaceURI 的所有子代元素的清單。

GetElementsByTagName(String)

傳回 XmlNodeList,其中包含符合指定 Name 之所有子代 (Descendant) 項目的清單。

GetElementsByTagName(String, String)

傳回 XmlNodeList,其中包含符合指定之 LocalNameNamespaceURI 的所有子代元素的清單。

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

要相符的區域名稱。 星號 (*) 是與所有標記相符的特殊值。

namespaceURI
String

要相符的命名空間 URI。

傳回

XmlNodeList,包含所有符合節點的清單。 如果沒有符合的節點,此清單會是空的。

備註

節點會依樹狀結構預先排序周遊 XmlElement 中遇到的順序排列。

注意

建議您使用 XmlNode.SelectNodesXmlNode.SelectSingleNode 方法, GetElementsByTagName 而不是 方法。

適用於

GetElementsByTagName(String)

傳回 XmlNodeList,其中包含符合指定 Name 之所有子代 (Descendant) 項目的清單。

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

要比對的名稱標記。 這是限定名稱。 它會與符合節點的 Name 屬性比對。 星號 (*) 是與所有標記相符的特殊值。

傳回

XmlNodeList,包含所有符合節點的清單。 如果沒有符合的節點,此清單會是空的。

範例

下列範例會取得並顯示所有書籍標題。

#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 ,作為輸入。

<!--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 中遇到的順序排列。

注意

建議您使用 XmlNode.SelectNodesXmlNode.SelectSingleNode 方法, GetElementsByTagName 而不是 方法。

適用於