NodeType
内容
[
隐藏
]Table.NodeType property
返回Table.
public override NodeType NodeType { get; }
例子
展示如何遍历复合节点的子节点树。
public void RecurseChildren()
{
Document doc = new Document(MyDir + "Paragraphs.docx");
// 任何可以包含子节点的节点(例如文档本身)都是复合节点。
Assert.True(doc.IsComposite);
// 调用递归函数,遍历并打印复合节点的所有子节点。
TraverseAllNodes(doc, 0);
}
/// <summary>
/// 递归遍历节点树,同时打印每个节点的类型
/// 缩进取决于深度以及所有内联节点的内容。
/// </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)}");
// 如果该节点是复合节点,则递归进入该节点。否则,如果该节点是内联节点,则打印其内容。
if (childNode.IsComposite)
{
Console.WriteLine();
TraverseAllNodes((CompositeNode)childNode, depth + 1);
}
else if (childNode is Inline)
{
Console.WriteLine($" - \"{childNode.GetText().Trim()}\"");
}
else
{
Console.WriteLine();
}
}
}
显示如何确定表是否嵌套。
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];
// 查明表格中是否有单元格具有其他表格作为子表格。
int count = GetChildTableCount(table);
Console.WriteLine("Table #{0} has {1} tables directly within its cells", i, count);
// 查明表是否嵌套在另一个表中,如果是,则嵌套深度是多少。
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>
/// 计算一个表嵌套在其他表中的级别。
/// </summary>
/// <returns>
/// 一个整数,表示表的嵌套深度(父表节点的数量)。
/// </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>
/// 确定表的单元格内是否包含任何直接子表。
/// 不要递归遍历这些表来检查进一步的表。
/// </summary>
/// <returns>
/// 如果至少有一个子单元格包含表格,则返回 true。
/// 如果表格中没有单元格包含表格,则返回 false。
/// </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;
}
也可以看看
- enum NodeType
- class Table
- 命名空间 Aspose.Words.Tables
- 部件 Aspose.Words