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

注釈

タグ間に内容のない開始タグと終了タグを含む要素は、空の要素とは見なされないことに注意してください。 長さを持たないコンテンツがあります。 開始タグのみを含み、終了する空の要素として表される要素のみが空と見なされます。

適用対象

こちらもご覧ください