XmlElement.GetAttribute 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定特性的特性值。
重载
| GetAttribute(String) | 
						   返回具有指定名称的属性的值。  | 
        	
| GetAttribute(String, String) | 
						   返回具有指定本地名称和命名空间 URI 的特性的值。  | 
        	
GetAttribute(String)
返回具有指定名称的属性的值。
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
 
要检索的属性的名称。 这是限定名。 它针对匹配节点的 Name 属性进行匹配。
返回
指定的属性的值。 如果未找到匹配属性,或者如果此属性没有指定值或默认值,则返回空字符串。
示例
以下示例检查元素是否具有指定的属性。
#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)
返回具有指定本地名称和命名空间 URI 的特性的值。
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
	参数
- localName
 - String
 
要检索的特性的本地名称。
- namespaceURI
 - String
 
要检索的特性的命名空间 URI。
返回
指定的属性的值。 如果未找到匹配属性,或者如果此属性没有指定值或默认值,则返回空字符串。