NodeGetAncestor(NodeType) Method

Gets the first ancestor of the specified NodeType.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.8.0


public CompositeNode GetAncestor(
	NodeType ancestorType


Type: Aspose.WordsNodeType
The node type of the ancestor to retrieve.

Return Value

Type: CompositeNode
The ancestor of the specified type or null if no ancestor of this type was found.


Shows how to find out if a table contains another table or if the table itself is nested inside another table.
public void CalculateDepthOfNestedTables()
    Document doc = new Document(MyDir + "Nested tables.docx");
    NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);

    for (int i = 0; i < tables.Count; i++)
        // First lets find if any cells in the table have tables themselves as children
        int count = GetChildTableCount((Table)tables[i]);
        Console.WriteLine("Table #{0} has {1} tables directly within its cells", i, count);

        // Now let's try the other way around, lets try find if the table is nested inside another table and at what depth
        int tableDepth = GetNestedDepthOfTable((Table)tables[i]);

        if (tableDepth > 0)
            Console.WriteLine("Table #{0} is nested inside another table at depth of {1}", i,
            Console.WriteLine("Table #{0} is a non nested table (is not a child of another table)", i);

/// <summary>
/// Calculates what level a table is nested inside other tables.
/// <returns>
/// An integer containing the level the table is nested at.
/// 0 = Table is not nested inside any other table
/// 1 = Table is nested within one parent table
/// 2 = Table is nested within two parent tables etc..</returns>
/// </summary>
private static int GetNestedDepthOfTable(Table table)
    int depth = 0;

    // The parent of the table will be a Cell, instead attempt to find a grandparent that is of type Table
    Node parent = table.GetAncestor(table.NodeType);

    while (parent != null)
        // Every time we find a table a level up we increase the depth counter and then try to find an
        // ancestor of type table from the parent
        parent = parent.GetAncestor(typeof(Table));

    return depth;

/// <summary>
/// Determines if a table contains any immediate child table within its cells.
/// Does not recursively traverse through those tables to check for further tables.
/// <returns>Returns true if at least one child cell contains a table.
/// Returns false if no cells in the table contains a table.</returns>
/// </summary>
private static int GetChildTableCount(Table table)
    int tableCount = 0;
    // Iterate through all child rows in the table
    foreach (Row row in table.Rows.OfType<Row>())
        // Iterate through all child cells in the row
        foreach (Cell Cell in row.Cells.OfType<Cell>())
            // Retrieve the collection of child tables of this cell
            TableCollection childTables = Cell.Tables;

            // If this cell has a table as a child then return true
            if (childTables.Count > 0)

    // No cell contains a table
    return tableCount;

See Also