NodeGetAncestor Method (NodeType)
Gets the first ancestor of the specified NodeType.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.9
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.

Document doc = new Document(MyDir + "Table.NestedTables.doc");
    int tableIndex = 0;

    foreach (Table table in doc.GetChildNodes(NodeType.Table, true).OfType<Table>())
        // First lets find if any cells in the table have tables themselves as children.
        int count = GetChildTableCount(table);
        Console.WriteLine("Table #{0} has {1} tables directly within its cells", tableIndex, 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);

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


/// <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;

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

    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(type);

    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