Поделиться через


XmlReader.IsEmptyElement Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public MustOverride ReadOnly Property IsEmptyElement As Boolean
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.

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.

Examples

Dim output As New StringBuilder()

Dim xmlString As String = _
        "<book>" & _
            "<title>Pride And Prejudice</title>" & _
            "<price>19.95</price>" & _
            "<misc/>" & _
        "</book>"

' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

    While reader.Read()
        Select Case reader.NodeType
            Case XmlNodeType.Element
                If reader.IsEmptyElement Then
                    output.AppendLine(Environment.NewLine + "<" + reader.Name + "/>")
                Else
                    output.Append("<" + reader.Name + ">")
                End If
            Case XmlNodeType.Text
                output.Append(reader.Value)
            Case XmlNodeType.Document
            Case XmlNodeType.EndElement
                output.AppendLine("</" + reader.Name + ">")
        End Select
    End While
End Using
OutputTextBlock.Text = output.ToString()

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

Version Information

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

Platforms

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