The most efficient way to access child nodes of a CompositeNode is via the Node.FirstChild and Node.LastChild properties that return the first and last child nodes respectively. If there are no child nodes, a null is returned.
CompositeNode also provides the ChildNodes collection that allows indexed or enumerated access to the children. The ChildNodes property is a live collection of nodes. It means that whenever the document is changed (nodes removed or inserted), the ChildNodes collection is automatically updated. Node collections are discussed in detail in further topics.
If a node has no children, then ChildNodes returns an empty collection. You can check if a CompositeNode contains any child nodes using the HasChildNodes property.
Example ChildNodesForEach
Shows how to enumerate immediate children of a CompositeNode using the enumerator provided by the ChildNodes collection.
[C#]
NodeCollection children = paragraph.ChildNodes;
foreach (Node child in children)
{
// Paragraph may contain children of various types such as runs, shapes and so on.
if (child.NodeType.Equals(NodeType.Run))
{
// Say we found the node that we want, do something useful.
Run run = (Run)child;
Console.WriteLine(run.Text);
}
}
[Visual Basic]
Dim children As NodeCollection = paragraph.ChildNodes
For Each child As Node In children
' Paragraph may contain children of various types such as runs, shapes and so on.
If child.NodeType.Equals(NodeType.Run) Then
' Say we found the node that we want, do something useful.
Dim run As Run = CType(child, Run)
Console.WriteLine(run.Text)
End If
Next child
[Java]
NodeCollection<Node> children = paragraph.getChildNodes();
for (Node child : children)
{
// Paragraph may contain children of various types such as runs, shapes and so on.
if (child.getNodeType() == NodeType.RUN)
{
// Say we found the node that we want, do something useful.
Run run = (Run)child;
System.out.println(run.getText());
}
}
Example ChildNodesIndexer
Shows how to enumerate immediate children of a CompositeNode using indexed access.
[C#]
NodeCollection children = paragraph.ChildNodes;
for (int i = 0; i < children.Count; i++)
{
Node child = children[i];
// Paragraph may contain children of various types such as runs, shapes and so on.
if (child.NodeType.Equals(NodeType.Run))
{
// Say we found the node that we want, do something useful.
Run run = (Run)child;
Console.WriteLine(run.Text);
}
}
[Visual Basic]
Dim children As NodeCollection = paragraph.ChildNodes
Dim i As Integer = 0
Do While i < children.Count
Dim child As Node = children(i)
' Paragraph may contain children of various types such as runs, shapes and so on.
If child.NodeType.Equals(NodeType.Run) Then
' Say we found the node that we want, do something useful.
Dim run As Run = CType(child, Run)
Console.WriteLine(run.Text)
End If
i += 1
Loop
[Java]
NodeCollection children = paragraph.getChildNodes();
for (int i = 0; i < children.getCount(); i++)
{
Node child = children.get(i);
// Paragraph may contain children of various types such as runs, shapes and so on.
if (child.getNodeType() == NodeType.RUN)
{
// Say we found the node that we want, do something useful.
Run run = (Run)child;
System.out.println(run.getText());
}
}