Читати англійською Редагувати

Поділитися через


XmlElement.GetElementsByTagName Method

Definition

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

Overloads

GetElementsByTagName(String, String)

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

GetElementsByTagName(String)

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

GetElementsByTagName(String, String)

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

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

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

Parameters

localName
String

The local name to match. The asterisk (*) is a special value that matches all tags.

namespaceURI
String

The namespace URI to match.

Returns

An XmlNodeList containing a list of all matching nodes. The list is empty if there are no matching nodes.

Remarks

The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

Примітка

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

Applies to

.NET 10 та інші версії
Продукт Версії
.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, 10
.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)

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

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

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

Parameters

name
String

The name tag to match. This is a qualified name. It is matched against the Name property of the matching node. The asterisk (*) is a special value that matches all tags.

Returns

An XmlNodeList containing a list of all matching nodes. The list is empty if there are no matching nodes.

Examples

The following example gets and displays all the book titles.

C#
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);
     }
  }
}

The example uses the file, 2books.xml, as input.

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>

Remarks

The nodes are placed in the order in which they would be encountered in a preorder traversal of the XmlElement tree.

Примітка

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

Applies to

.NET 10 та інші версії
Продукт Версії
.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, 10
.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