Proprietà XmlTextReader.IsEmptyElement (System.Xml)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Proprietà XmlTextReader.IsEmptyElement

Ottiene un valore che indica se il nodo corrente è un elemento vuoto, ad esempio <MyElement/> .

Spazio dei nomi: System.Xml
Assembly: System.Xml (in system.xml.dll)

Sintassi

Visual Basic - (Dichiarazione)
Public Overrides ReadOnly Property IsEmptyElement As Boolean
Visual Basic (Utilizzo)
Dim instance As XmlTextReader
Dim value As Boolean

value = instance.IsEmptyElement

C#
public override bool IsEmptyElement { get; }
C++
public:
virtual property bool IsEmptyElement {
	bool get () override;
}
J#
/** @property */
public boolean get_IsEmptyElement ()

JScript
public override function get IsEmptyElement () : boolean

Valore proprietà

true se il nodo corrente rappresenta un elemento (proprietà NodeType uguale a XmlNodeType.Element) che termina con />; in caso contrario, false.
Note

NotaNota

Nella versione Microsoft .NET Framework versione 2.0 è consigliabile creare istanze di XmlReader utilizzando il metodo System.Xml.XmlReader.Create. In questo modo è possibile sfruttare completamente le nuove funzionalità introdotte in questa versione. Per ulteriori informazioni, vedere Creazione di lettori XML.

Questa proprietà consente di determinare la differenza tra le seguenti stringhe:

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

<item num="123">, IsEmptyElement è false, nonostante l'elemento sia vuoto.

Per gli elementi vuoti non viene generato alcun nodo EndElement corrispondente.

IsEmptyElement riporta semplicemente se l'elemento nel documento di origine dispone di un tag di fine dell'elemento.

Esempio

Nell'esempio seguente viene riportato il contenuto di testo di ciascun elemento.

Visual Basic
Option Strict
Option Explicit

Imports System
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 'Main 
End Class 'Sample

C#
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

C++
#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();
   }

}


J#
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        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.get_IsEmptyElement()) {
                        Console.WriteLine("<{0}/>", reader.get_Name());
                    }
                    else {
                        Console.Write("<{0}> ", reader.get_Name());
                        reader.Read(); //Read the start tag.
                        if (reader.IsStartElement()) { //Handle nested elements.
                            Console.Write("\r\n<{0}>", reader.get_Name());
                        }
                        //Read the text content of the element.
                        Console.WriteLine(reader.ReadString()); 
                    }
                }
            }
        }     
        finally {
            if (reader != null) {
                reader.Close();
            }
        }
    } //main
} //End class Sample

Nell'esempio viene utilizzato il file elems.xml come input.


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

Piattaforme

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0 1.1 1.0

.NET Compact Framework

Supportato in: 2.0 1.0
Vedere anche