XElement.IsEmpty Property

Definition

Gets a value indicating whether this element contains no content.

public:
 property bool IsEmpty { bool get(); };
public bool IsEmpty { get; }
member this.IsEmpty : bool
Public ReadOnly Property IsEmpty As Boolean

Property Value

Boolean

true if this element contains no content; otherwise false.

Examples

The following example creates a variety of XML trees, and shows the value of this property with each tree.

XElement el1 = new XElement("Root");  
Console.WriteLine(el1);  
Console.WriteLine(el1.IsEmpty);  
Console.WriteLine();  
XElement el2 = new XElement("Root", "content");  
Console.WriteLine(el2);  
Console.WriteLine(el2.IsEmpty);  
Console.WriteLine();  
XElement el3 = new XElement("Root", "");  
Console.WriteLine(el3);  
Console.WriteLine(el3.IsEmpty);  
Console.WriteLine();  
el3.ReplaceAll(null);  
Console.WriteLine(el3);  
Console.WriteLine(el3.IsEmpty);  
Dim el1 As XElement = <Root/>  
Console.WriteLine(el1)  
Console.WriteLine(el1.IsEmpty)  
Console.WriteLine()  
Dim el2 As XElement = <Root>content</Root>  
Console.WriteLine(el2)  
Console.WriteLine(el2.IsEmpty)  
Console.WriteLine()  
Dim el3 As XElement = <Root></Root>  
Console.WriteLine(el3)  
Console.WriteLine(el3.IsEmpty)  
Console.WriteLine()  
el3.ReplaceAll(Nothing)  
Console.WriteLine(el3)  
Console.WriteLine(el3.IsEmpty)  

This example produces the following output:

<Root />  
True  

<Root>content</Root>  
False  

<Root></Root>  
False  

<Root />  
True  

Remarks

Note that an element that contains a start and end tag with no content between the tags is not considered to be an empty element. It has content with no length. Only an element that contains only a start tag, and is expressed as a terminated empty element, is considered to be empty.

Applies to

See also