XmlNodeReader.IsEmptyElement Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient une valeur indiquant si le nœud actuel est un élément vide (par exemple). <MyElement/>
public:
virtual property bool IsEmptyElement { bool get(); };
public override bool IsEmptyElement { get; }
member this.IsEmptyElement : bool
Public Overrides ReadOnly Property IsEmptyElement As Boolean
Valeur de propriété
true si le nœud actuel est un élément (NodeType égal XmlNodeType.Element) et qu’il se termine par />; sinon, false.
Exemples
L’exemple suivant affiche le contenu texte de chaque élément.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlNodeReader reader = null;
try
{
//Create and load the XML document.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
"<title>Pride And Prejudice</title>" +
"<price>19.95</price>" +
"<misc/>" +
"</book>");
//Load the XmlNodeReader
reader = new XmlNodeReader(doc);
//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 XmlNodeReader = Nothing
Try
'Create and load the XML document.
Dim doc As New XmlDocument()
doc.LoadXml("<book>" & _
"<title>Pride And Prejudice</title>" & _
"<price>19.95</price>" & _
"<misc/>" & _
"</book>")
'Load the XmlNodeReader
reader = New XmlNodeReader(doc)
'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
Remarques
Note
Pour tirer parti des fonctionnalités plus récentes, la pratique recommandée consiste à créer XmlReader des instances à l’aide de la XmlReaderSettings classe et de la Create méthode. Pour plus d’informations, consultez la section Remarques dans la XmlReader page de référence.
Cette propriété vous permet de déterminer la différence entre les éléments suivants :
<item num="123"/> (IsEmptyElement est true).
<item num="123"> (IsEmptyElement est false, bien que le contenu de l’élément soit vide).
Un nœud correspondant EndElement n’est pas généré pour les éléments vides.