NodeType
Contents
[
Hide
]Table.NodeType property
Returns Table.
public override NodeType NodeType { get; }
Examples
Shows how to traverse a composite node’s tree of child nodes.
public void RecurseChildren()
{
Document doc = new Document(MyDir + "Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
Assert.True(doc.IsComposite);
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
TraverseAllNodes(doc, 0);
}
/// <summary>
/// Recursively traverses a node tree while printing the type of each node
/// with an indent depending on depth as well as the contents of all inline nodes.
/// </summary>
public void TraverseAllNodes(CompositeNode parentNode, int depth)
{
for (Node childNode = parentNode.FirstChild; childNode != null; childNode = childNode.NextSibling)
{
Console.Write($"{new string('\t', depth)}{Node.NodeTypeToString(childNode.NodeType)}");
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode.IsComposite)
{
Console.WriteLine();
TraverseAllNodes((CompositeNode)childNode, depth + 1);
}
else if (childNode is Inline)
{
Console.WriteLine($" - \"{childNode.GetText().Trim()}\"");
}
else
{
Console.WriteLine();
}
}
}
Shows how to find out if a tables are nested.
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++)
{
Table table = (Table)tables[i];
// Find out if any cells in the table have other tables as children.
int count = GetChildTableCount(table);
Console.WriteLine("Table #{0} has {1} tables directly within its cells", i, count);
// Find out if the table is nested inside another table, and, if so, at what depth.
int tableDepth = GetNestedDepthOfTable(table);
if (tableDepth > 0)
Console.WriteLine("Table #{0} is nested inside another table at depth of {1}", i,
tableDepth);
else
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.
/// </summary>
/// <returns>
/// An integer indicating the nesting depth of the table (number of parent table nodes).
/// </returns>
private static int GetNestedDepthOfTable(Table table)
{
int depth = 0;
Node parent = table.GetAncestor(table.NodeType);
while (parent != null)
{
depth++;
parent = parent.GetAncestor(typeof(Table));
}
return depth;
}
/// <summary>
/// Determines if a table contains any immediate child table within its cells.
/// Do not recursively traverse through those tables to check for further tables.
/// </summary>
/// <returns>
/// Returns true if at least one child cell contains a table.
/// Returns false if no cells in the table contain a table.
/// </returns>
private static int GetChildTableCount(Table table)
{
int childTableCount = 0;
foreach (Row row in table.Rows)
{
foreach (Cell Cell in row.Cells)
{
TableCollection childTables = Cell.Tables;
if (childTables.Count > 0)
childTableCount++;
}
}
return childTableCount;
}
See Also
- enum NodeType
- class Table
- namespace Aspose.Words.Tables
- assembly Aspose.Words