XElement.IsEmpty 属性

定义

获取一个值,该值指示此元素是否不包含内容。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

属性值

如果元素不包含内容,则为 true;否则为 falsetrue if this element contains no content; otherwise false.

示例

下面的示例创建了各种 XML 树,并显示了每个树的此属性的值。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  

注解

请注意,包含标记之间没有内容的开始标记和结束标记的元素不会被视为空元素。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.

适用于

另请参阅