Επεξεργασία

Κοινή χρήση μέσω


XmlReader.IsEmptyElement Property

Definition

When overridden in a derived class, gets a value indicating whether the current node is an empty element (for example, <MyElement/>).

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

Property Value

true if the current node is an element (NodeType equals XmlNodeType.Element) that ends with />; otherwise, false.

Exceptions

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

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)
    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

The example uses the file, elems.xml, as input.

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

Remarks

This property enables you to determine the difference between the following:

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

<item num="123"></item> (IsEmptyElement is false, although element content is empty).

A corresponding EndElement node is not generated for empty elements.

If 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. In other words, IsEmptyElement simply reports whether or not the element in the source document had an end element tag.

Applies to