TableCollection Class

Provides typed access to a collection of Table nodes.

Inheritance Hierarchy

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0


public class TableCollection : NodeCollection

The TableCollection type exposes the following members.


Public propertyCode exampleCount
Gets the number of nodes in the collection.
(Inherited from NodeCollection.)
Public propertyCode exampleItem
Retrieves a Table at the given index.


Public methodCode exampleAdd
Adds a node to the end of the collection.
(Inherited from NodeCollection.)
Public methodCode exampleClear
Removes all nodes from this collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleContains
Determines whether a node is in the collection.
(Inherited from NodeCollection.)
Public methodEquals (Inherited from Object.)
Public methodGetEnumerator
Provides a simple "foreach" style iteration over the collection of nodes.
(Inherited from NodeCollection.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleIndexOf
Returns the zero-based index of the specified node.
(Inherited from NodeCollection.)
Public methodCode exampleInsert
Inserts a node into the collection at the specified index.
(Inherited from NodeCollection.)
Public methodCode exampleRemove
Removes the node from the collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleRemoveAt
Removes the node at the specified index from the collection and from the document.
(Inherited from NodeCollection.)
Public methodCode exampleToArray
Copies all tables from the collection to a new array of tables.
Public methodToString (Inherited from Object.)


Shows how to use typed properties to access nodes of the document tree.
Document doc = new Document(MyDir + "Tables.docx");

// Quick typed access to all Table child nodes contained in the Body
TableCollection tables = doc.FirstSection.Body.Tables;

Assert.AreEqual(5, tables[0].Rows.Count);
Assert.AreEqual(4, tables[1].Rows.Count);

foreach (Table table in tables.OfType<Table>())
    // Quick typed access to the first row of the table

    // Quick typed access to the last row of the table

// Each table has shrunk by two rows
Assert.AreEqual(3, tables[0].Rows.Count);
Assert.AreEqual(2, tables[1].Rows.Count);
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