XmlNode.HasChildNodes 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出這個節點是否有子節點。
public:
virtual property bool HasChildNodes { bool get(); };
public virtual bool HasChildNodes { get; }
member this.HasChildNodes : bool
Public Overridable ReadOnly Property HasChildNodes As Boolean
屬性值
如果該節點有子節點,則為 true
,否則為 false
。
範例
下列範例會顯示根項目的所有子節點。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<book ISBN='1-861001-57-5'>"
"<title>Pride And Prejudice</title>"
"<price>19.95</price>"
"</book>" );
XmlNode^ root = doc->FirstChild;
//Display the contents of the child nodes.
if ( root->HasChildNodes )
{
for ( int i = 0; i < root->ChildNodes->Count; i++ )
{
Console::WriteLine( root->ChildNodes[ i ]->InnerText );
}
}
}
using System;
using System.Xml;
public class Sample2
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"<price>19.95</price>" +
"</book>");
XmlNode root = doc.FirstChild;
//Display the contents of the child nodes.
if (root.HasChildNodes)
{
for (int i = 0; i < root.ChildNodes.Count; i++)
{
Console.WriteLine(root.ChildNodes[i].InnerText);
}
}
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As New XmlDocument()
doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"<price>19.95</price>" & _
"</book>")
Dim root As XmlNode = doc.FirstChild
'Display the contents of the child nodes.
If root.HasChildNodes Then
Dim i As Integer
For i = 0 To root.ChildNodes.Count - 1
Console.WriteLine(root.ChildNodes(i).InnerText)
Next i
End If
End Sub
End Class
備註
如需子節點的相關資訊,請參閱 W3C DOM (Core) 層級 1 規格