XmlTextReader.GetAttribute Método

Definición

Obtiene el valor de un atributo.

Sobrecargas

GetAttribute(Int32)

Obtiene el valor del atributo con el índice especificado.

GetAttribute(String)

Obtiene el valor del atributo con el nombre especificado.

GetAttribute(String, String)

Obtiene el valor del atributo con el nombre local y el identificador URI de espacio de nombres que se hayan especificado.

Comentarios

Nota:

A partir de .NET Framework 2.0, se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.

GetAttribute(Int32)

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

Obtiene el valor del atributo con el índice especificado.

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

Parámetros

i
Int32

Índice del atributo. El índice está basado en cero. El primer atributo tiene índice 0.

Devoluciones

Valor del atributo especificado.

Excepciones

El parámetro i es menor que cero o mayor o igual que AttributeCount.

Comentarios

Nota:

A partir de .NET Framework 2.0, se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.

Este método no desplaza el lector.

Consulte también

Se aplica a

GetAttribute(String)

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

Obtiene el valor del atributo con el nombre especificado.

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

Parámetros

name
String

Nombre completo del atributo.

Devoluciones

Valor del atributo especificado. Si no se encuentra el atributo, se devuelve null.

Ejemplos

En el ejemplo siguiente se obtiene el valor del atributo 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

En el ejemplo se usa el archivo , attrs.xmlcomo entrada.


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

Comentarios

Nota:

A partir de .NET Framework 2.0, se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.

Este método no desplaza el lector.

Si el lector está colocado en un DocumentType nodo, este método se puede usar para obtener los literales PUBLIC y SYSTEM, por ejemplo, reader.GetAttribute("PUBLIC")

Consulte también

Se aplica a

GetAttribute(String, String)

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

Obtiene el valor del atributo con el nombre local y el identificador URI de espacio de nombres que se hayan especificado.

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

Parámetros

localName
String

Nombre local del atributo.

namespaceURI
String

URI de espacio de nombres del atributo.

Devoluciones

Valor del atributo especificado. Si no se encuentra el atributo, se devuelve null. Este método no desplaza el lector.

Comentarios

Nota:

A partir de .NET Framework 2.0, se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.

El siguiente XML contiene un atributo en un espacio de nombres específico:

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

Puede buscar el dt:type atributo mediante un argumento (prefijo y nombre local) o dos argumentos (nombre local y URI del espacio de nombres):

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

Para buscar el xmlns:dt atributo, use uno de los argumentos siguientes:

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

También puede obtener esta información mediante la Prefix propiedad .

Consulte también

Se aplica a