XmlElement.GetElementsByTagName 方法

定义

返回一个 XmlNodeList,它包含与指定名称匹配的所有子代元素的列表。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 匹配的所有子代元素的列表。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

XmlNodeList,包含所有匹配节点的列表。An XmlNodeList containing a list of all matching nodes. 如果不存在任何匹配节点,则该列表为空。The list is empty if there are no matching nodes.

注解

节点按其在树的前序遍历中遇到的顺序放置 XmlElementThe nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

备注

建议使用 XmlNode.SelectNodesXmlNode.SelectSingleNode 方法,而不是 GetElementsByTagName 方法。It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

适用于

GetElementsByTagName(String)

返回一个 XmlNodeList,它包含与指定 Name 匹配的所有子代元素的列表。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

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>

注解

节点按其在树的前序遍历中遇到的顺序放置 XmlElementThe nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

备注

建议使用 XmlNode.SelectNodesXmlNode.SelectSingleNode 方法,而不是 GetElementsByTagName 方法。It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

适用于