XmlReader.IsEmptyElement XmlReader.IsEmptyElement XmlReader.IsEmptyElement XmlReader.IsEmptyElement Property


当在派生类中被重写时,获取一个值,该值指示当前节点是否为空元素(例如 <MyElement/>)。When overridden in a derived class, gets a value indicating whether the current node is an empty element (for example, <MyElement/>).

 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则为; 否则为。true if the current node is an element (NodeType equals XmlNodeType.Element) that ends with />; otherwise, false.


在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."


下面的示例显示每个元素的文本内容。The following example displays the text content of each element.

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)
      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作为输入。The example uses the file, elems.xml, as input.

  <title>Pride And Prejudice</title>


此属性使您能够确定下面各项之间的差异:This property enables you to determine the difference between the following:

<item num="123"/>IsEmptyElementtrue)。<item num="123"/> (IsEmptyElement is true).

<item num="123"></item>IsEmptyElementfalse,但元素内容为空)。<item num="123"></item> (IsEmptyElement is false, although element content is empty).

不会EndElement为空元素生成相应的节点。A corresponding EndElement node is not generated for empty elements.

如果由于架构验证而将默认内容添加到元素, IsEmptyElement则仍返回。 trueIf default content has been added to an element due to schema validation, IsEmptyElement still returns true. 它与元素是否具有默认值无关。It has no bearing on whether or not the element has a default value. 换言之, IsEmptyElement只会报告源文档中的元素是否具有结束元素标记。In other words, IsEmptyElement simply reports whether or not the element in the source document had an end element tag.