XmlReader.HasValue Property
Summary
Gets a value indicating whether the current node has a value.
This property is `true` for attribute, CDATA, text, and element nodes.
Syntax
public abstract bool HasValue { get; }
Property Value
Type | Description |
---|---|
System.Boolean | true if the node has a value; otherwise, false . |
Remarks
The `HasValue` property returns true
for the following node types:
XmlNodeType.Attribute
XmlNodeType.CDATA
XmlNodeType.Text
XmlNodeType.Element
(when it has text content or attributes)
For other node types, such as XmlNodeType.Document
, XmlNodeType.Element
(without text content), or XmlNodeType.EndElement
, this property returns false
.
Important: This property should be checked after ensuring the reader is positioned on a node that can potentially have a value. Accessing it on an invalid node type might lead to unexpected behavior.
Examples
C# Example
using System;
using System.Xml;
public class XmlReaderExample
{
public static void Main()
{
string xml = @"<root><item id='1'>Hello</item></root>";
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
while (reader.Read())
{
Console.WriteLine($"Node Type: {reader.NodeType}, Name: {reader.Name}");
if (reader.HasValue)
{
Console.WriteLine($" Value: {reader.Value}");
}
}
}
}
}
Output:
Node Type: Element, Name: root
Node Type: Attribute, Name: id
Value: 1
Node Type: Text, Name: #text
Value: Hello
Node Type: EndElement, Name: item
Node Type: EndElement, Name: root
Requirements
Interface | Implementation |
---|---|
XmlReader.HasValue |
The `XmlReader` class implements this property. |
.NET Framework | Supported in versions 1.0 and later. |
.NET Core | Supported in .NET Core 1.0 and later. |