XElement.IsEmpty 属性

定义

获取一个值,该值指示此元素是否不包含任何内容。

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

属性值

true 如果此元素不包含任何内容,则为 ;否则 false

示例

以下示例创建各种 XML 树,并显示此属性的值以及每个树的值。

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)

此示例生成以下输出:

<Root />
True

<Root>content</Root>
False

<Root></Root>
False

<Root />
True

注解

请注意,包含标记之间没有内容的开始和结束标记的元素不被视为空元素。 它具有长度不长的内容。 仅包含开始标记且表示为已终止的空元素的元素被视为空。

适用于

另请参阅