语言

XmlElement.GetElementsByTagName 方法

定义

返回一个 XmlNodeList 包含与指定名称匹配的所有后代元素的列表。

重载

名称 说明
GetElementsByTagName(String, String)

返回一个 XmlNodeList 包含与指定 LocalName 和 NamespaceURI匹配的所有子代元素的列表。

GetElementsByTagName(String)

返回一个 XmlNodeList 包含与指定 Name匹配的所有子代元素的列表。

GetElementsByTagName(String, String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

返回一个 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 预排序遍历中遇到它们的顺序排列。

注释

建议使用 XmlNode.SelectNodes 或 XmlNode.SelectSingleNode 方法而不是 GetElementsByTagName 方法。

适用于

GetElementsByTagName(String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

返回一个 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 预排序遍历中遇到它们的顺序排列。

注释

建议使用 XmlNode.SelectNodes 或 XmlNode.SelectSingleNode 方法而不是 GetElementsByTagName 方法。

适用于