XmlTextReader.IsEmptyElement Tulajdonság

Definíció

Beolvas egy értéket, amely jelzi, hogy az aktuális csomópont üres elem-e (például <MyElement/>).

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

Tulajdonság értéke

trueha az aktuális csomópont egy elem (NodeType egyenlő XmlNodeType.Element) , amely a következővel />végződik: ; egyébként. false

Példák

Az alábbi példa az egyes elemek szöveges tartalmát jeleníti meg.

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

A példa a fájlt elems.xmlhasználja bemenetként.


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

Megjegyzések

Note

Javasoljuk, hogy az új funkciók kihasználásához hozzon létre XmlReader példányokat a XmlReader.Create metódus használatával.

Ez a tulajdonság lehetővé teszi, hogy meghatározza a különbséget a következők között:

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

<item num="123"> (IsEmptyElement az false, bár az elem tartalma üres).

A rendszer nem hoz létre megfelelő EndElement csomópontot üres elemekhez.

IsEmptyElement egyszerűen jelenti, hogy a forrásdokumentum eleme rendelkezik-e végfelhasználói címkével.

A következőre érvényes:

Lásd még