XmlTextReader.GetAttribute Metoda

Definicja

Pobiera wartość atrybutu.

Przeciążenia

GetAttribute(Int32)

Pobiera wartość atrybutu z określonym indeksem.

GetAttribute(String)

Pobiera wartość atrybutu o określonej nazwie.

GetAttribute(String, String)

Pobiera wartość atrybutu o określonej lokalnej nazwie i identyfikatorze URI przestrzeni nazw.

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

GetAttribute(Int32)

Pobiera wartość atrybutu z określonym indeksem.

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

Indeks atrybutu. Jest to indeks zaczynający się od zera. (Pierwszy atrybut ma indeks 0).

Zwraca

String

Wartość określonego atrybutu.

Wyjątki

Parametr i jest mniejszy niż 0 lub większy niż lub równy AttributeCount.

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

Ta metoda nie przenosi czytnika.

Zobacz też

Dotyczy

GetAttribute(String)

Pobiera wartość atrybutu o określonej nazwie.

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

Kwalifikowana nazwa atrybutu.

Zwraca

String

Wartość określonego atrybutu. Jeśli atrybut nie zostanie znaleziony, null zostanie zwrócony.

Przykłady

Poniższy przykład pobiera wartość atrybutu 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

W przykładzie użyto pliku , attrs.xmljako danych wejściowych.


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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

Ta metoda nie przenosi czytnika.

Jeśli czytnik jest umieszczony w węźle DocumentType , ta metoda może służyć do pobierania literałów PUBLIC i SYSTEM, na przykład, reader.GetAttribute("PUBLIC")

Zobacz też

Dotyczy

GetAttribute(String, String)

Pobiera wartość atrybutu o określonej lokalnej nazwie i identyfikatorze URI przestrzeni nazw.

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

Lokalna nazwa atrybutu.

namespaceURI
String

Identyfikator URI przestrzeni nazw atrybutu.

Zwraca

String

Wartość określonego atrybutu. Jeśli atrybut nie zostanie znaleziony, null zostanie zwrócony. Ta metoda nie przenosi czytnika.

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

Następujący kod XML zawiera atrybut w określonej przestrzeni nazw:

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

Atrybut można wyszukać przy użyciu jednego argumentu dt:type (prefiksu i nazwy lokalnej) lub dwóch argumentów (nazwa lokalna i identyfikator URI przestrzeni nazw):

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

Aby wyszukać xmlns:dt atrybut, użyj jednego z następujących argumentów:

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

Te informacje można również uzyskać przy użyciu Prefix właściwości .

Zobacz też

Dotyczy