XmlValidatingReader.IsEmptyElement Vlastnost

Definice

Získá hodnotu označující, zda je aktuální uzel prázdný prvek (například <MyElement/>).

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

Hodnota vlastnosti

truepokud je aktuální uzel elementem (rovná se XmlNodeType.Element), který končí na />; v opačném případě false.NodeType

Příklady

Následující příklad zobrazuje textový obsah každého prvku.

#using <System.Xml.dll>

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

}

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
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
End Class

V příkladu se jako vstup používá soubor elems.xml, .

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

Poznámky

Poznámka

Třída XmlValidatingReader je zastaralá v rozhraní .NET Framework 2.0. Ověřovací instanci můžete vytvořit XmlReader pomocí XmlReaderSettings třídy a Create metody. Další informace najdete v části Poznámky na XmlReader referenční stránce.

Tato vlastnost umožňuje určit rozdíl mezi následujícími vlastnostmi:

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

<item num="123"> (IsEmptyElement je false, i když obsah elementu je prázdný).

Odpovídající EndElement uzel se negeneruje pro prázdné prvky.

Pokud byl výchozí obsah přidán do elementu kvůli ověření schématu, IsEmptyElement stále vrátí true. Nemá žádný vliv na to, jestli má prvek výchozí hodnotu. Jinými slovy jednoduše hlásí, IsEmptyElement jestli měl prvek ve zdrojovém dokumentu značku koncového prvku.

Platí pro

Viz také