XmlElement.GetElementsByTagName 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 和 XmlNodeList 包含所有與指定名稱相符後裔元素的清單。
多載
| 名稱 | Description |
|---|---|
| GetElementsByTagName(String, String) |
回傳 和 XmlNodeList 包含所有符合指定 LocalName 和 NamespaceURI的後裔元素列表。 |
| GetElementsByTagName(String) |
回傳 和 XmlNodeList 包含所有與指定 Name元素相符的後裔元素列表。 |
GetElementsByTagName(String, String)
回傳 和 XmlNodeList 包含所有符合指定 LocalName 和 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
當地的名稱也相符。 星號(*)是一個特殊值,適用於所有標籤。
- namespaceURI
- String
命名空間的 URI 也符合。
傳回
XmlNodeList包含所有匹配節點的清單。 若沒有匹配節點,則該清單為空。
備註
節點會依照在樹的預序遍歷 XmlElement 中會遇到的順序排列。
Note
建議你使用 XmlNode.SelectNodes 「或方法 XmlNode.SelectSingleNode 」而非「方法 GetElementsByTagName 」。
適用於
GetElementsByTagName(String)
回傳 和 XmlNodeList 包含所有與指定 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
名牌也配對。 這是一個有條件的名稱。 它會與 Name 匹配節點的性質進行匹配。 星號(*)是一個特殊值,適用於所有標籤。
傳回
XmlNodeList包含所有匹配節點的清單。 若沒有匹配節點,則該清單為空。
範例
以下範例會取得並顯示所有書籍名稱。
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 中會遇到的順序排列。
Note
建議你使用 XmlNode.SelectNodes 「或方法 XmlNode.SelectSingleNode 」而非「方法 GetElementsByTagName 」。