語言

XmlElement.HasAttribute 方法

定義

判斷目前節點是否擁有指定屬性。

多載

名稱 Description
HasAttribute(String)

判斷目前節點是否有指定名稱的屬性。

HasAttribute(String, String)

判斷目前節點是否有具有指定本地名稱與命名空間 URI 的屬性。

HasAttribute(String)

來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs

判斷目前節點是否有指定名稱的屬性。

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

要找屬性的名字。 這是一個有條件的名稱。 它會與 Name 匹配節點的性質進行匹配。

傳回

true如果目前節點擁有指定的屬性;否則,。 false

範例

以下範例檢查該元素是否具備指定的屬性。

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)

來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs
來源:
XmlElement.cs

判斷目前節點是否有具有指定本地名稱與命名空間 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

要找屬性的當地名稱。

namespaceURI
String

要尋找屬性的命名空間 URI。

傳回

true如果目前節點擁有指定的屬性;否則,。 false

適用於