XmlDocument.GetElementsByTagName Method

Definition

Returns an XmlNodeList containing a list of all descendant elements that match the specified name.

Overloads

GetElementsByTagName(String)

Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.

GetElementsByTagName(String, String)

Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.

GetElementsByTagName(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.

public virtual System.Xml.XmlNodeList GetElementsByTagName (string name);

Parameters

name
String

The qualified name to match. It is matched against the Name property of the matching node. The special value "*" matches all tags.

Returns

An XmlNodeList containing a list of all matching nodes. If no nodes match name, the returned collection will be empty.

Examples

The following example creates a XmlDocument object and uses the GetElementsByTagName method and the resulting XmlNodeList object to display all the book titles.

using System;
using System.Xml;

public class Sample1
{
    public static void Main()
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load("books.xml");

        //Display all the book titles.
        XmlNodeList elemList = doc.GetElementsByTagName("title");
        for (int i = 0; i < elemList.Count; i++)
        {
            Console.WriteLine(elemList[i].InnerXml);
        }
    }
}

The example uses the books.xml file as input.

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

Remarks

The nodes are placed in the order in which they would be encountered in the document.

备注

It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

GetElementsByTagName(String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.

public virtual System.Xml.XmlNodeList GetElementsByTagName (string localName, string namespaceURI);

Parameters

localName
String

The LocalName to match. The special value "*" matches all tags.

namespaceURI
String

NamespaceURI to match.

Returns

An XmlNodeList containing a list of all matching nodes. If no nodes match the specified localName and namespaceURI, the returned collection will be empty.

Remarks

The nodes are placed in the order in which they would be encountered in the document tree.

备注

It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method.

Applies to

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0