Export (0) Print
Expand All

XmlReader.IsEmptyElement Property

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

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public abstract bool IsEmptyElement { get; }

Property Value

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

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.



StringBuilder output = new StringBuilder();

String xmlString =
    @"<book>
      <title>Pride And Prejudice</title>
      <price>19.95</price>
      <misc/>
  </book>";

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                if (reader.IsEmptyElement)
                    output.AppendLine(Environment.NewLine + "<" + reader.Name + "/>");
                else
                    output.Append("<" + reader.Name + ">");
                break;
            case XmlNodeType.Text:
                output.Append(reader.Value);
                break;
            case XmlNodeType.Document:
                break;
            case XmlNodeType.EndElement:
                output.AppendLine("</" + reader.Name + ">");
                break;
        }

    }
}
OutputTextBlock.Text = output.ToString();


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft