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

GetAttribute(Int32)

Pobiera wartość atrybutu z określonym indeksem.

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

Parametry

attributeIndex
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

W .NET Framework 2.0 zalecaną praktyką jest utworzenie XmlReader wystąpień przy użyciu XmlReaderSettings klasy i Create metody. Dzięki temu można w pełni wykorzystać wszystkie nowe funkcje wprowadzone w .NET Framework. Aby uzyskać więcej informacji, zobacz sekcję Uwagi na stronie referencyjnej XmlReader .

Ta metoda nie przenosi czytnika.

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()
{
   XmlNodeReader^ reader = nullptr;
   try
   {
      
      //Create and load the XML document.
      XmlDocument^ doc = gcnew XmlDocument;
      doc->LoadXml( "<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> "
      "</book>" );
      
      // Load the XmlNodeReader 
      reader = gcnew XmlNodeReader( doc );
      
      //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()
  {
    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

Uwagi

Uwaga

W .NET Framework 2.0 zalecaną praktyką jest utworzenie XmlReader wystąpień przy użyciu XmlReaderSettings klasy i Create metody. Dzięki temu można w pełni wykorzystać wszystkie nowe funkcje wprowadzone w .NET Framework. Aby uzyskać więcej informacji, zobacz sekcję Uwagi na stronie referencyjnej XmlReader .

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")

Dotyczy

GetAttribute(String, String)

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

public:
 override System::String ^ GetAttribute(System::String ^ name, System::String ^ namespaceURI);
public override string? GetAttribute (string name, 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

Parametry

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

Uwagi

Uwaga

W .NET Framework 2.0 zalecaną praktyką jest utworzenie XmlReader wystąpień przy użyciu XmlReaderSettings klasy i Create metody. Dzięki temu można w pełni wykorzystać wszystkie nowe funkcje wprowadzone w .NET Framework. Aby uzyskać więcej informacji, zobacz sekcję Uwagi na stronie referencyjnej XmlReader .

Poniższy 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 .

Dotyczy