XmlNodeReader.IsEmptyElement 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出目前的節點是否為空項目 (例如 <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.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlNodeReader^ reader = nullptr;
try
{
//Create and load the XML document.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<book>"
"<title>Pride And Prejudice</title>"
"<price>19.95</price>"
"<misc/>"
"</book>" );
//Load the XmlNodeReader
reader = gcnew 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 != nullptr )
reader->Close();
}
}
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
備註
注意
在 .NET Framework 2.0 中,建議的做法是使用 XmlReaderSettings 類別和 Create 方法建立 XmlReader 實例。 這可讓您充分利用.NET Framework中引進的所有新功能。 如需詳細資訊,請參閱參考頁面中的 XmlReader 一節。
此屬性可讓您判斷下列各項目之間的差異:
<item num="123"/>``IsEmptyElement
() true
。
<item num="123">``IsEmptyElement
(為 false
,雖然元素內容是空的) 。
不會為空白元素產生對應的 EndElement
節點。