XmlElement.HasAttribute XmlElement.HasAttribute XmlElement.HasAttribute XmlElement.HasAttribute Method

定義

判斷目前的節點是否有指定的屬性。Determines whether the current node has the specified attribute.

多載

HasAttribute(String) HasAttribute(String) HasAttribute(String) HasAttribute(String)

判斷目前的節點是否具有指定名稱的屬性。Determines whether the current node has an attribute with the specified name.

HasAttribute(String, String) HasAttribute(String, String) HasAttribute(String, String) HasAttribute(String, String)

判斷目前的節點是否有具有指定區域名稱和命名空間 URI 的屬性。Determines whether the current node has an attribute with the specified local name and namespace URI.

HasAttribute(String) HasAttribute(String) HasAttribute(String) HasAttribute(String)

判斷目前的節點是否具有指定名稱的屬性。Determines whether the current node has an attribute with the specified name.

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

參數

name
String String String String

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

傳回

如果目前節點有指定的屬性,則為 true;否則為 falsetrue if the current node has the specified attribute; otherwise, false.

範例

下列範例會檢查項目是否具有指定的屬性。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

HasAttribute(String, String) HasAttribute(String, String) HasAttribute(String, String) HasAttribute(String, String)

判斷目前的節點是否有具有指定區域名稱和命名空間 URI 的屬性。Determines whether the current node has an attribute with the specified local name and namespace URI.

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

參數

localName
String String String String

要尋找的屬性的區域名稱。The local name of the attribute to find.

namespaceURI
String String String String

要尋找的屬性的命名空間 URI。The namespace URI of the attribute to find.

傳回

如果目前節點有指定的屬性,則為 true;否則為 falsetrue if the current node has the specified attribute; otherwise, false.

適用於