次の方法で共有


XmlDocument.GetElementsByTagName メソッド (String)

指定した Name に一致するすべての子孫の要素のリストを格納している XmlNodeList を返します。

Overloads Public Overridable Function GetElementsByTagName( _
   ByVal name As String _) As XmlNodeList
[C#]
public virtual XmlNodeList GetElementsByTagName(stringname);
[C++]
public: virtual XmlNodeList* GetElementsByTagName(String* name);
[JScript]
public function GetElementsByTagName(
   name : String) : XmlNodeList;

パラメータ

  • name
    一致する限定名。一致するノードの Name プロパティに一致します。特殊値の "*" は、すべてのタグに一致します。

戻り値

一致しているすべてのノードのリストを格納している XmlNodeList

解説

ノードは、ドキュメントで検出される順序で配置されます。

使用例

[Visual Basic, C#, C++] XmlDocument オブジェクトを作成し、 GetElmentsByTagName メソッドとその結果の XmlNodeList オブジェクトを使用して、すべての書籍のタイトルを表示する例を次に示します。

 
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  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);
    }  

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->Load(S"books.xml");

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

この例では、入力として、 books.xml というファイルを使用しています。

<?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>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlDocument クラス | XmlDocument メンバ | System.Xml 名前空間 | XmlDocument.GetElementsByTagName オーバーロードの一覧