XmlElement.GetAttribute Método

Definição

Retorna o valor de atributo para o atributo especificado.

Sobrecargas

GetAttribute(String)

Retorna o valor do atributo com o nome especificado.

GetAttribute(String, String)

Retorna o valor do atributo com o nome local e o URI do namespace especificados.

GetAttribute(String)

Retorna o valor do atributo com o nome especificado.

public:
 virtual System::String ^ GetAttribute(System::String ^ name);
public virtual string GetAttribute (string name);
abstract member GetAttribute : string -> string
override this.GetAttribute : string -> string
Public Overridable Function GetAttribute (name As String) As String

Parâmetros

name
String

O nome do atributo a ser recuperado. Este é um nome qualificado. Ele é comparado com a propriedade Name do nó correspondente.

Retornos

String

O valor do atributo especificado. Uma cadeia de caracteres vazia será retornada se um atributo correspondente não for encontrado ou se o atributo não tiver um valor especificado ou padrão.

Exemplos

O exemplo a seguir verifica se o elemento tem o atributo especificado.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   XmlElement^ root = doc->DocumentElement;
   
   // Check to see if the element has a genre attribute.
   if ( root->HasAttribute( "genre" ) )
   {
      String^ genre = root->GetAttribute( "genre" );
      Console::WriteLine( genre );
   }
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    // Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre")){
      String genre = root.GetAttribute("genre");
      Console.WriteLine(genre);
   }
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    Dim root as XmlElement = doc.DocumentElement

    ' Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre"))
     Dim genre as String = root.GetAttribute("genre")
     Console.WriteLine(genre)
    end if
       
    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.InnerXml)

  end sub
end class

Aplica-se a

GetAttribute(String, String)

Retorna o valor do atributo com o nome local e o URI do namespace especificados.

public:
 virtual System::String ^ GetAttribute(System::String ^ localName, System::String ^ namespaceURI);
public virtual string GetAttribute (string localName, string namespaceURI);
public virtual string GetAttribute (string localName, string? namespaceURI);
abstract member GetAttribute : string * string -> string
override this.GetAttribute : string * string -> string
Public Overridable Function GetAttribute (localName As String, namespaceURI As String) As String

Parâmetros

localName
String

O nome local do atributo a ser recuperado.

namespaceURI
String

O URI do namespace do atributo a ser recuperado.

Retornos

String

O valor do atributo especificado. Uma cadeia de caracteres vazia será retornada se um atributo correspondente não for encontrado ou se o atributo não tiver um valor especificado ou padrão.

Aplica-se a