XmlElement.GetAttribute XmlElement.GetAttribute XmlElement.GetAttribute XmlElement.GetAttribute Method

定義

傳回指定屬性的屬性值。Returns the attribute value for the specified attribute.

多載

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

傳回具有指定名稱之屬性的值。Returns the value for the attribute with the specified name.

GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String)

傳回具有指定區域名稱和命名空間 URI 之屬性的值。Returns the value for the attribute with the specified local name and namespace URI.

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

傳回具有指定名稱之屬性的值。Returns the value for the attribute with the specified name.

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

參數

name
String String String String

要擷取之屬性的名稱。The name of the attribute to retrieve. 這是限定名稱。This is a qualified name. 它會與符合節點的 Name 屬性比對。It is matched against the Name property of the matching node.

傳回

指定的屬性值。The value of the specified attribute. 如果找不到相符的屬性,或者屬性沒有指定的或預設的值,會傳回空字串。An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.

範例

下列範例會檢查項目是否具有指定的屬性。The following example checks to see if the element has the specified attribute.

#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

GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String)

傳回具有指定區域名稱和命名空間 URI 之屬性的值。Returns the value for the attribute with the specified local name and namespace URI.

public:
 virtual System::String ^ GetAttribute(System::String ^ localName, System::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

參數

localName
String String String String

要擷取的屬性的區域名稱。The local name of the attribute to retrieve.

namespaceURI
String String String String

要擷取的屬性的命名空間 URI。The namespace URI of the attribute to retrieve.

傳回

指定的屬性值。The value of the specified attribute. 如果找不到相符的屬性,或者屬性沒有指定的或預設的值,會傳回空字串。An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.

適用於