XmlNodeReader.get_HasAttributes Property

Namespace: System.Xml
Assembly: System.Xml.dll

Syntax

public virtual bool get_HasAttributes { get; }

Description

Gets a value indicating whether the current node has attributes.

Note: This property returns true if the current node is an element node and has attributes. Otherwise, it returns false.

Property Value

bool

true if the current node has attributes; otherwise, false.

Remarks

The XmlNodeReader class allows you to read XML data as a sequence of XmlNodeType nodes. The get_HasAttributes property is a convenient way to check for the presence of attributes on the current node without having to explicitly check the node type and then access attribute information.

Example

The following example demonstrates how to use the get_HasAttributes property to check if an element node has attributes.

using System; using System.Xml; public class Example { public static void Main(string[] args) { string xmlString = @"<root attribute1=""value1"" attribute2=""value2""> <child>Some text</child> </root>"; using (XmlReader reader = new XmlNodeReader(xmlString)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { Console.WriteLine($"Node: {reader.Name}"); if (reader.HasAttributes) { Console.WriteLine(" This element has attributes."); for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); Console.WriteLine($" Attribute: {reader.Name} = {reader.Value}"); } } else { Console.WriteLine(" This element has no attributes."); } } } } } }

See Also