XmlTextReader.IsEmptyElement Propriedade

Definição

Obtém um valor que indica se o nó atual é um elemento vazio (por exemplo, <MyElement/>).Gets a value indicating whether the current node is an empty element (for example, <MyElement/>).

public:
 virtual property bool IsEmptyElement { bool get(); };
public override bool IsEmptyElement { get; }
member this.IsEmptyElement : bool
Public Overrides ReadOnly Property IsEmptyElement As Boolean

Valor da propriedade

Boolean

true Se o nó atual for um elemento ( NodeType Equals XmlNodeType.Element ) que termina com /> ; caso contrário, false .true if the current node is an element (NodeType equals XmlNodeType.Element) that ends with />; otherwise, false.

Exemplos

O exemplo a seguir exibe o conteúdo de texto de cada elemento.The following example displays the text content of each element.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew XmlTextReader( "elems.xml" );
      
      //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 != nullptr )
            reader->Close();
   }

}

using System;
using System.IO;
using System.Xml;

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

    try
    {
       //Load the reader with the XML file.
       reader = new XmlTextReader("elems.xml");

       //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
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("elems.xml")
            
            '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
End Class

O exemplo usa o arquivo, elems.xml , como entrada.The example uses the file, elems.xml, as input.


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

Comentários

Observação

A partir do .NET Framework 2,0, recomendamos que você crie XmlReader instâncias usando o XmlReader.Create método para aproveitar a nova funcionalidade.Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

Essa propriedade permite determinar a diferença entre o seguinte:This property enables you to determine the difference between the following:

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

<item num="123"> ( IsEmptyElement é false , embora o conteúdo do elemento esteja vazio).<item num="123"> (IsEmptyElement is false, although element content is empty).

Um EndElement nó correspondente não é gerado para elementos vazios.A corresponding EndElement node is not generated for empty elements.

IsEmptyElement simplesmente relata se o elemento no documento de origem tinha uma marca de elemento final.IsEmptyElement simply reports whether or not the element in the source document had an end element tag.

Aplica-se a

Confira também