語言

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

備註

請注意,包含起始與結束標籤且標籤間無內容的元素,不被視為空元素。 內容沒有長度。 只有只包含起始標籤且以終止空元素表示的元素,才被視為空的。

適用於

另請參閱