XmlTextReader.IsEmptyElement Свойство
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает значение, указывающее, является ли текущий узел пустым элементом (например, <MyElement/>).
public:
virtual property bool IsEmptyElement { bool get(); };
public override bool IsEmptyElement { get; }
member this.IsEmptyElement : bool
Public Overrides ReadOnly Property IsEmptyElement As Boolean
Значение свойства
true Значение , если текущий узел является элементом (NodeType равно XmlNodeType.Element) и заканчивается />; в противном случае false.
Примеры
В следующем примере отображается текстовое содержимое каждого элемента.
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
В этом примере файл elems.xmlиспользуется в качестве входных данных.
<book>
<title>Pride And Prejudice</title>
<price>19.95</price>
<misc/>
</book>
Комментарии
Note
Рекомендуем создавать XmlReader экземпляры с помощью XmlReader.Create метода, чтобы воспользоваться новыми функциями.
Это свойство позволяет определить разницу между следующими:
<item num="123"/> (IsEmptyElement имеет trueзначение ).
<item num="123">( имеет IsEmptyElementзначение ,false хотя содержимое элемента пусто).
Соответствующий EndElement узел не создается для пустых элементов.
IsEmptyElement просто сообщает, был ли элемент в исходном документе тегом конечного элемента.