Прочетете на английски Редактиране

Споделяне чрез


XmlElement.HasAttribute Method

Definition

Determines whether the current node has the specified attribute.

Overloads

HasAttribute(String)

Determines whether the current node has an attribute with the specified name.

HasAttribute(String, String)

Determines whether the current node has an attribute with the specified local name and namespace URI.

HasAttribute(String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

Determines whether the current node has an attribute with the specified name.

C#
public virtual bool HasAttribute(string name);

Parameters

name
String

The name of the attribute to find. This is a qualified name. It is matched against the Name property of the matching node.

Returns

true if the current node has the specified attribute; otherwise, false.

Examples

The following example checks to see if the element has the specified attribute.

C#
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);
   }
  }
}

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

HasAttribute(String, String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

Determines whether the current node has an attribute with the specified local name and namespace URI.

C#
public virtual bool HasAttribute(string localName, string namespaceURI);
C#
public virtual bool HasAttribute(string localName, string? namespaceURI);

Parameters

localName
String

The local name of the attribute to find.

namespaceURI
String

The namespace URI of the attribute to find.

Returns

true if the current node has the specified attribute; otherwise, false.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0