XmlTextReader.GetAttribute Metoda

Definice

Získá hodnotu atributu.

Přetížení

GetAttribute(Int32)

Získá hodnotu atributu se zadaným indexem.

GetAttribute(String)

Získá hodnotu atributu se zadaným názvem.

GetAttribute(String, String)

Získá hodnotu atributu se zadaným místním názvem a identifikátorem URI oboru názvů.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework 2.0 doporučujeme vytvářet XmlReader instance pomocí XmlReader.Create metody, abyste mohli využívat nové funkce.

GetAttribute(Int32)

Získá hodnotu atributu se zadaným indexem.

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

Parametry

i
Int32

Index atributu. Index je založený na nule. (První atribut má index 0.)

Návraty

String

Hodnota zadaného atributu.

Výjimky

Parametr i je menší než 0 nebo větší než nebo rovno AttributeCount.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework 2.0 doporučujeme vytvářet XmlReader instance pomocí XmlReader.Create metody, abyste mohli využívat nové funkce.

Tato metoda nepřesune čtečku.

Viz také

Platí pro

GetAttribute(String)

Získá hodnotu atributu se zadaným názvem.

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

Parametry

name
String

Kvalifikovaný název atributu.

Návraty

String

Hodnota zadaného atributu. Pokud atribut nebyl nalezen, null vrátí se.

Příklady

Následující příklad získá hodnotu atributu 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

Příklad používá soubor , attrs.xmljako vstup.


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

Poznámky

Poznámka

Počínaje rozhraním .NET Framework 2.0 doporučujeme vytvářet XmlReader instance pomocí XmlReader.Create metody, abyste mohli využívat nové funkce.

Tato metoda nepřesune čtečku.

Pokud je čtečka umístěna DocumentType na uzlu, lze tuto metodu použít k získání literálů PUBLIC a SYSTEM, například reader.GetAttribute("PUBLIC")

Viz také

Platí pro

GetAttribute(String, String)

Získá hodnotu atributu se zadaným místním názvem a identifikátorem URI oboru názvů.

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

Parametry

localName
String

Místní název atributu.

namespaceURI
String

Identifikátor URI oboru názvů atributu.

Návraty

String

Hodnota zadaného atributu. Pokud atribut nebyl nalezen, null vrátí se. Tato metoda nepřesune čtečku.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework 2.0 doporučujeme vytvářet XmlReader instance pomocí XmlReader.Create metody, abyste mohli využívat nové funkce.

Následující XML obsahuje atribut v určitém oboru názvů:

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

Atribut můžete vyhledat pomocí jednoho argumentu dt:type (předpony a místního názvu) nebo dvou argumentů (identifikátor URI místního názvu a oboru názvů):

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

Pokud chcete vyhledat xmlns:dt atribut, použijte jeden z následujících argumentů:

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

Tyto informace můžete získat také pomocí Prefix vlastnosti.

Viz také

Platí pro