XmlTextReader.GetAttribute メソッド

定義

属性の値を取得します。

オーバーロード

GetAttribute(Int32)

指定したインデックスの属性の値を取得します。

GetAttribute(String)

指定した名前の属性の値を取得します。

GetAttribute(String, String)

指定したローカル名および名前空間 URI に関連付けられた属性の値を取得します。

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

GetAttribute(Int32)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

指定したインデックスの属性の値を取得します。

public:
 override System::String ^ GetAttribute(int i);
public override string GetAttribute (int i);
override this.GetAttribute : int -> string
Public Overrides Function GetAttribute (i As Integer) As String

パラメーター

i
Int32

属性のインデックス。 インデックスの値は、0 から始まります。 最初の属性のインデックスは 0 です。

戻り値

指定した属性の値。

例外

i パラメーターが 0 未満か、AttributeCount 以上です。

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

このメソッドは、リーダーを移動しません。

こちらもご覧ください

適用対象

GetAttribute(String)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

指定した名前の属性の値を取得します。

public:
 override System::String ^ GetAttribute(System::String ^ name);
public override string? GetAttribute (string name);
public override string GetAttribute (string name);
override this.GetAttribute : string -> string
Public Overrides Function GetAttribute (name As String) As String

パラメーター

name
String

属性の限定名。

戻り値

指定した属性の値。 指定した属性が見つからない場合は null が返されます。

次の例では、ISBN 属性の値を取得します。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew XmlTextReader( "attrs.xml" );
      
      //Read the ISBN attribute.
      reader->MoveToContent();
      String^ isbn = reader->GetAttribute( "ISBN" );
      Console::WriteLine( "The ISBN value: {0}", isbn );
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlTextReader reader = null;

    try
    {
       //Load the reader with the XML file.
       reader = new XmlTextReader("attrs.xml");

       //Read the ISBN attribute.
       reader.MoveToContent();
       string isbn = reader.GetAttribute("ISBN");
       Console.WriteLine("The ISBN value: " + isbn);
     }
     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("attrs.xml")
            
            'Read the ISBN attribute.
            reader.MoveToContent()
            Dim isbn As String = reader.GetAttribute("ISBN")
            Console.WriteLine("The ISBN value: " & isbn)
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

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


<book genre='novel' ISBN='1-861003-78' pubdate='1987'>
</book>

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

このメソッドは、リーダーを移動しません。

リーダーがノードに DocumentType 配置されている場合、このメソッドを使用して PUBLIC リテラルと SYSTEM リテラルを取得できます。たとえば、 reader.GetAttribute("PUBLIC")

こちらもご覧ください

適用対象

GetAttribute(String, String)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

指定したローカル名および名前空間 URI に関連付けられた属性の値を取得します。

public:
 override System::String ^ GetAttribute(System::String ^ localName, System::String ^ namespaceURI);
public override string? GetAttribute (string localName, string? namespaceURI);
public override string GetAttribute (string localName, string namespaceURI);
override this.GetAttribute : string * string -> string
Public Overrides Function GetAttribute (localName As String, namespaceURI As String) As String

パラメーター

localName
String

属性のローカル名。

namespaceURI
String

属性の名前空間 URI。

戻り値

指定した属性の値。 指定した属性が見つからない場合は null が返されます。 このメソッドは、リーダーを移動しません。

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

次の XML には、特定の名前空間の 属性が含まれています。

<test xmlns:dt="urn:datatypes" dt:type="int"/>

属性は dt:type 、1 つの引数 (プレフィックスとローカル名) または 2 つの引数 (ローカル名と名前空間 URI) を使用して検索できます。

String dt = reader.GetAttribute("dt:type");
String dt2 = reader.GetAttribute("type","urn:datatypes");

属性を xmlns:dt 検索するには、次のいずれかの引数を使用します。

String dt3 = reader.GetAttribute("xmlns:dt");
String dt4 = reader.GetAttribute("dt",http://www.w3.org/2000/xmlns/);

この情報は、 プロパティを使用して Prefix 取得することもできます。

こちらもご覧ください

適用対象