CompositeNodeFirstChild Property

Gets the first child of the node.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public Node FirstChild { get; }

Property Value

Type: Node
Remarks
If there is no first child node, a null is returned.
Examples
Shows how to enumerate immediate child nodes of a composite node using NextSibling.
// In this example we enumerate all paragraphs of a section body
// Get the section that we want to work on
Section section = doc.Sections[0];
Body body = section.Body;

// Loop starting from the first child until we reach null
for (Node node = body.FirstChild; node != null; node = node.NextSibling)
{
    // Output the types of the nodes that we come across
    Console.WriteLine(Node.NodeTypeToString(node.NodeType));
}
Examples
Shows how to efficiently visit all direct and indirect children of a composite node.
public void RecurseAllNodes()
{
    // Open a document
    Document doc = new Document(MyDir + "Document.docx");

    // Invoke the recursive function that will walk the tree
    TraverseAllNodes(doc);
}

/// <summary>
/// A simple function that will walk through all children of a specified node recursively 
/// and print the type of each node to the screen.
/// </summary>
public void TraverseAllNodes(CompositeNode parentNode)
{
    // This is the most efficient way to loop through immediate children of a node
    for (Node childNode = parentNode.FirstChild; childNode != null; childNode = childNode.NextSibling)
    {
        // Do some useful work
        Console.WriteLine(Node.NodeTypeToString(childNode.NodeType));

        // Recurse into the node if it is a composite node
        if (childNode.IsComposite)
            TraverseAllNodes((CompositeNode) childNode);
    }
}
See Also