Nested tables in a word document

I am parsing word documents using OpenXml that can contain nested tables but I am having problems locating the tables within each other.

I cannot seem to use table.Descendents<Table>() because if the parent table has more than one table nested inside regardless of where it is located then they are all a descendant. Same if I got to row or cell level I cant use decendents to find the table if there is more than one.

I have tried code like this

foreach (TableRow row in table.Descendants<TableRow>())

{

foreach (TableCell cell in row.Descendants<TableCell>())

{

if (cell.Descendants<Table>().Count() > 0)

{

foreach (Table cellTable in cell.Descendants<Table>())

{

TraverseRowsToLastTable(cellTable, unityObjectData, doc);

}

}

else

{

// CloneRowNodes(table, ubjectData, doc);

}

}

}

but when it returns to the cell or row within table then it runs through the for each for all the rows that are a descendant of table and that includes the ones in the nested tables that have already been dealt with.

How am i meant to go through a table, look at one row at a time then look at each cell one at a time and if there are any tables within the cell then go back and look at them? Has anyone seen a link on the net regarding nested tables and openXml?

Hi, I forgot I posted this on here but I have solved the problem. What I did was find the table/s with the most ancestors and kept a note of the count. I then dealt with all the tables that had an Ancestor count of that number then reduced the number by 1 and dealt with those tables until I came back to my table with no ancestors which was therefore at the top of the nest.