XmlDocument.GetElementsByTagName Metodo

Definizione

Restituisce un oggetto XmlNodeList contenente un elenco di tutti gli elementi discendenti che corrispondono al nome specificato.

Overload

Nome Descrizione
GetElementsByTagName(String)

Restituisce un oggetto XmlNodeList contenente un elenco di tutti gli elementi discendenti corrispondenti all'oggetto specificato Name.

GetElementsByTagName(String, String)

Restituisce un oggetto XmlNodeList contenente un elenco di tutti gli elementi discendenti che corrispondono all'oggetto e LocalNamespecificatoNamespaceURI.

GetElementsByTagName(String)

Restituisce un oggetto XmlNodeList contenente un elenco di tutti gli elementi discendenti corrispondenti all'oggetto specificato 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

Parametri

name
String

Nome completo da trovare. Viene confrontato con la Name proprietà del nodo corrispondente. Il valore speciale "*" corrisponde a tutti i tag.

Valori restituiti

Oggetto XmlNodeList contenente un elenco di tutti i nodi corrispondenti. Se nessun nodo corrisponde namea , la raccolta restituita sarà vuota.

Esempio

Nell'esempio seguente viene creato un XmlDocument oggetto e viene utilizzato il GetElementsByTagName metodo e l'oggetto risultante XmlNodeList per visualizzare tutti i titoli dei libri.

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);
        }
    }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.Load("books.xml")
        
        'Display all the book titles.
        Dim elemList As XmlNodeList = doc.GetElementsByTagName("title")
        Dim i As Integer
        For i = 0 To elemList.Count - 1
            Console.WriteLine(elemList(i).InnerXml)
        Next i
    End Sub
End Class

L'esempio usa il books.xml file come 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>

Commenti

I nodi vengono inseriti nell'ordine in cui verrebbero rilevati nel documento.

Note

È consigliabile usare il XmlNode.SelectNodes metodo o XmlNode.SelectSingleNode anziché il GetElementsByTagName metodo .

Si applica a

GetElementsByTagName(String, String)

Restituisce un oggetto XmlNodeList contenente un elenco di tutti gli elementi discendenti che corrispondono all'oggetto e LocalNamespecificatoNamespaceURI.

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

Parametri

localName
String

LocalName da trovare. Il valore speciale "*" corrisponde a tutti i tag.

namespaceURI
String

NamespaceURI per la corrispondenza.

Valori restituiti

Oggetto XmlNodeList contenente un elenco di tutti i nodi corrispondenti. Se nessun nodo corrisponde all'oggetto specificato localName e namespaceURI, la raccolta restituita sarà vuota.

Commenti

I nodi vengono inseriti nell'ordine in cui verrebbero rilevati nell'albero dei documenti.

Note

È consigliabile usare il XmlNode.SelectNodes metodo o XmlNode.SelectSingleNode anziché il GetElementsByTagName metodo .

Si applica a