XmlElement.HasAttribute 方法

定义

确定当前节点是否具有指定的特性。Determines whether the current node has the specified attribute.

重载

HasAttribute(String)

确定当前节点是否具有带有指定名称的属性。Determines whether the current node has an attribute with the specified name.

HasAttribute(String, String)

确定当前节点是否具有带有指定本地名称和命名空间 URI 的特性。Determines whether the current node has an attribute with the specified local name and namespace URI.

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

要查找的属性的名称。The name of the attribute to find. 这是限定名。This is a qualified name. 它针对匹配节点的 Name 属性进行匹配。It is matched against the Name property of the matching node.

返回

Boolean

如果当前节点具有指定的属性,则为 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)

确定当前节点是否具有带有指定本地名称和命名空间 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);
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

要查找的特性的本地名称。The local name of the attribute to find.

namespaceURI
String

要查找的特性的命名空间 URI。The namespace URI of the attribute to find.

返回

Boolean

如果当前节点具有指定的属性,则为 true;否则为 falsetrue if the current node has the specified attribute; otherwise, false.

适用于