XmlNodeReader.GetAttribute メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
属性の値を取得します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetAttribute(Int32) |
指定したインデックスを持つ属性の値を取得します。 |
| GetAttribute(String) |
指定した名前の属性の値を取得します。 |
| GetAttribute(String, String) |
指定したローカル名と名前空間 URI を持つ属性の値を取得します。 |
GetAttribute(Int32)
指定したインデックスを持つ属性の値を取得します。
public:
override System::String ^ GetAttribute(int attributeIndex);
public override string GetAttribute(int attributeIndex);
override this.GetAttribute : int -> string
Public Overrides Function GetAttribute (attributeIndex As Integer) As String
パラメーター
- attributeIndex
- Int32
属性のインデックス。 インデックスは 0 から始まります。 (最初の属性のインデックスは 0 です)。
返品
指定した属性の値。
例外
attributeIndex が 0 未満か、 AttributeCount以上です。
注釈
XmlReader クラスと XmlReaderSettings メソッドを使用して、Create インスタンスを作成することをお勧めします。
このメソッドはリーダーを移動しません。
適用対象
GetAttribute(String)
指定した名前の属性の値を取得します。
public:
override System::String ^ GetAttribute(System::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;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " +
"</book>");
// Load the XmlNodeReader
reader = new XmlNodeReader(doc);
//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 XmlNodeReader = Nothing
Try
'Create and load the XML document.
Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " & _
"</book>")
' Load the XmlNodeReader
reader = New XmlNodeReader(doc)
'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
注釈
Note
新しい機能を利用するには、XmlReader クラスと XmlReaderSettings メソッドを使用してCreate インスタンスを作成することをお勧めします。 詳細については、「 XmlReader リファレンス」ページの「解説」セクションを参照してください。
このメソッドはリーダーを移動しません。
リーダーが DocumentType ノードに配置されている場合、このメソッドを使用して PUBLIC リテラルと SYSTEM リテラルを取得できます。たとえば、 reader.GetAttribute("PUBLIC")
適用対象
GetAttribute(String, String)
指定したローカル名と名前空間 URI を持つ属性の値を取得します。
public:
override System::String ^ GetAttribute(System::String ^ name, System::String ^ namespaceURI);
public override string GetAttribute(string name, string namespaceURI);
override this.GetAttribute : string * string -> string
Public Overrides Function GetAttribute (name As String, namespaceURI As String) As String
パラメーター
- name
- String
属性のローカル名。
- namespaceURI
- String
属性の名前空間 URI。
返品
指定した属性の値。 属性が見つからない場合は、 null が返されます。
注釈
Note
新しい機能を利用するには、XmlReader クラスと XmlReaderSettings メソッドを使用して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 プロパティを使用して取得することもできます。