次の方法で共有


XmlElement.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

解説

ノードは、 XmlElement ツリーの先行順走査で認識される順序で配置されます。

使用例

[Visual Basic, C#, C++] すべての書籍タイトルを取得し、表示する例を次に示します。

 
Imports System
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

[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);
     } 
    
  }
}

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

int main()
{
     XmlDocument* doc = new XmlDocument();
     doc->Load(S"2books.xml");
                         
     // Get and display all the book titles.
     XmlElement* root = doc->DocumentElement;
     XmlNodeList* elemList = root->GetElementsByTagName(S"title");
     for (int i=0; i < elemList->Count; i++)
     {   
        Console::WriteLine(elemList->ItemOf[i]->InnerXml);
     } 
}

この例では、入力として、 2books.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>

[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

参照

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