Count
内容
[
隐藏
]NodeCollection.Count property
获取集合中的节点数。
public int Count { get; }
例子
展示如何遍历复合节点的子节点集合。
Document doc = new Document();
// 将两个运行和一个形状作为子节点添加到此文档的第一段。
Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);
paragraph.AppendChild(new Run(doc, "Hello world! "));
Shape shape = new Shape(doc, ShapeType.Rectangle);
shape.Width = 200;
shape.Height = 200;
// 请注意,“CustomNodeId”不会保存到输出文件中,并且仅在节点生命周期内存在。
shape.CustomNodeId = 100;
shape.WrapType = WrapType.Inline;
paragraph.AppendChild(shape);
paragraph.AppendChild(new Run(doc, "Hello again!"));
// 遍历段落的直接子段落集合,
// 并打印我们在其中发现的任何运行或形状。
NodeCollection children = paragraph.GetChildNodes(NodeType.Any, false);
Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, false).Count);
foreach (Node child in children)
switch (child.NodeType)
{
case NodeType.Run:
Console.WriteLine("Run contents:");
Console.WriteLine($"\t\"{child.GetText().Trim()}\"");
break;
case NodeType.Shape:
Shape childShape = (Shape)child;
Console.WriteLine("Shape:");
Console.WriteLine($"\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}");
break;
}
显示如何确定表是否嵌套。
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;
}
也可以看看
- class NodeCollection
- 命名空间 Aspose.Words
- 部件 Aspose.Words