Language

XmlNode.HasChildNodes Property

Definition

Gets a value indicating whether this node has any child nodes.

public:
 virtual property bool HasChildNodes { bool get(); };
public virtual bool HasChildNodes { get; }
member this.HasChildNodes : bool
Public Overridable ReadOnly Property HasChildNodes As Boolean

Property Value

true if the node has child nodes; otherwise, false.

Examples

The following example displays all the child nodes of the root element.

using System;
using System.Xml;

public class Sample2
{
    public static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("" +
                    "Pride And Prejudice" +
                    "19.95" +
                    "");

        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("" & _
                    "Pride And Prejudice" & _
                    "19.95" & _
                    "")
        
        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

Remarks

For information about child nodes, see the W3C DOM (Core) Level 1 specification

Applies to