XmlReader.IsEmptyElement 属性

定义

在派生类中重写时,获取一个值,该值指示当前节点是否为空元素(例如)。 <MyElement/>

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

属性值

true如果当前节点是以结尾NodeType的元素(XmlNodeType.Element等于/>),则为 ;否则为 false

例外

在上一个异步操作完成之前调用了一个 XmlReader 方法。 在这种情况下, InvalidOperationException 会引发消息“正在进行异步操作”。

示例

以下示例显示每个元素的文本内容。

while (reader.Read()) {
  if (reader.IsStartElement()) {
    if (reader.IsEmptyElement)
                {
                    Console.WriteLine("<{0}/>", reader.Name);
                }
                else {
      Console.Write("<{0}> ", reader.Name);
      reader.Read(); // Read the start tag.
      if (reader.IsStartElement())  // Handle nested elements.
        Console.Write("\r\n<{0}>", reader.Name);
      Console.WriteLine(reader.ReadString());  //Read the text content of the element.
    }
  }
}
While reader.Read()
  If reader.IsStartElement() Then
    If reader.IsEmptyElement Then
      Console.WriteLine("<{0}/>", reader.Name)
    Else
      Console.Write("<{0}> ", reader.Name)
      reader.Read() ' Read the start tag.
      If reader.IsStartElement() Then ' Handle nested elements.
        Console.Write(vbCr + vbLf + "<{0}>", reader.Name)
      End If
      Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
    End If
  End If
End While

该示例使用该文件 elems.xml作为输入。

<book>
  <title>Pride And Prejudice</title>
  <price>19.95</price>
  <misc/>
</book>

注解

此属性使你能够确定以下各项之间的差异:

<item num="123"/>IsEmptyElementtrue)。

<item num="123"></item>IsEmptyElementfalse,虽然元素内容为空)。

不会为空元素生成相应的 EndElement 节点。

如果由于架构验证而已将默认内容添加到元素, IsEmptyElement 则仍返回 true。 它与元素是否具有默认值没有任何关系。 换句话说, IsEmptyElement 只需报告源文档中的元素是否具有结束元素标记。

适用于