This documentation is archived and is not being maintained.

XmlValidatingReader.IsEmptyElement Property

Gets a value indicating whether the current node is an empty element (for example, <MyElement/>).

[Visual Basic]
Overrides Public ReadOnly Property IsEmptyElement As Boolean
[C#]
public override bool IsEmptyElement {get;}
[C++]
public: __property bool get_IsEmptyElement();
[JScript]
public override function get IsEmptyElement() : Boolean;

Property Value

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"> (IsEmptyElement is false).

A corresponding EndElement node is not generated for empty elements.

Example

[Visual Basic, C#, C++] The following example displays the text content of each element.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
   
   Public Shared Sub Main()
      Dim txtreader As XmlTextReader = Nothing
      Dim reader As XmlValidatingReader = Nothing
      
      Try
         'Implement the readers.
         txtreader = New XmlTextReader("elems.xml")
         reader = New XmlValidatingReader(txtreader)
         
         'Parse the XML and display the text content of each of the elements.
         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())  'Handle nested elements.
                    Console.WriteLine()
                    Console.Write("<{0}>", reader.Name)
                  End If
                  Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
               End If
            End If
         End While      
      
      Finally
         If Not (reader Is Nothing) Then
            reader.Close()
         End If
      End Try
   End Sub 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample 
{
  public static void Main()
  {
    XmlTextReader txtreader = null;
    XmlValidatingReader reader = null;

    try
    {
       //Implement the readers.
       txtreader = new XmlTextReader("elems.xml");
       reader = new XmlValidatingReader(txtreader);
  
       //Parse the XML and display the text content of each of the elements.
       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.
           }
         }
       } 
       
     } 

     finally 
     {
        if (reader != null)
          reader.Close();
      }
  }
  
} // End class

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlTextReader* txtreader = 0;
   XmlValidatingReader* reader = 0;

   try
   {
      //Implement the readers.
      txtreader = new XmlTextReader(S"elems.xml");
      reader = new XmlValidatingReader(txtreader);

      //Parse the XML and display the text content of each of the elements.
      while (reader->Read()){
         if (reader->IsStartElement()){
            if (reader->IsEmptyElement)
               Console::WriteLine(S"<{0}/>",reader->Name);
            else{
               Console::Write(S"<{0}> ",reader->Name);
               reader->Read(); //Read the start tag.
               if (reader->IsStartElement())  //Handle nested elements.
                  Console::Write(S"\r\n<{0}>",reader->Name);
               Console::WriteLine(reader->ReadString());  //Read the text content of the element.
            }
         }
      } 

   } 

   __finally 
   {
      if (reader != 0)
         reader->Close();
   }
}

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlValidatingReader Class | XmlValidatingReader Members | System.Xml Namespace

Show: