How to know if a table is inside a table using the Aspose Word

I have a Word document.
a.docx (13 KB)

I want to get data from each table inside the table.
How to know if a table is inside a table using the Aspose Word.
Thank you!

@quanghieumylo, could you try the following code example and check whether it suits your needs:

Document doc = new Document("a.docx");

int tableIndex = 0;
foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{
    // Inner tables has cell as a parent node.
    if (t.ParentNode is Cell)
    {
        Console.WriteLine($"Inner table: {tableIndex}");
        GetTableData(t);
        tableIndex++;

        Console.WriteLine("");
    }
}


static void GetTableData(Table t)
{
    int rowIndex = 0;
    foreach (Row row in t.Rows)
    {
        int cellIndex = 0;
        foreach (Cell cell in row.Cells)
        {
            Console.WriteLine($"Row: {rowIndex} Cell: {cellIndex}");
            Console.WriteLine(cell.GetText());
            cellIndex++;
        }
        rowIndex++;
    }
}
1 Like

It is perfect, thanks! :smiling_face_with_three_hearts:

Hey bro, I have one more question.
How do I clone anything between the two words “table2” and “table3” into a new document?
a.docx (13.2 KB)

@quanghieumylo, I have replied you in this topic.

1 Like

Thank you. I thought I had to create a new topic to get a reply, so I posted it in two places.