CompositeNode.GetEnumerator method

Provides support for the for each style iteration over the child nodes of this node.

public IEnumerator<Node> GetEnumerator()


Shows how to traverse through a composite node’s collection of child nodes.

Document doc = new Document();

// Add two runs and one shape as child nodes to the first paragraph of this 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;
// Note that the 'CustomNodeId' is not saved to an output file and exists only during the node lifetime.
shape.CustomNodeId = 100;
shape.WrapType = WrapType.Inline;

paragraph.AppendChild(new Run(doc, "Hello again!"));

// Iterate through the paragraph's collection of immediate children,
// and print any runs or shapes that we find within.
NodeCollection children = paragraph.ChildNodes;

Assert.AreEqual(3, paragraph.ChildNodes.Count);

foreach (Node child in children)
    switch (child.NodeType)
        case NodeType.Run:
            Console.WriteLine("Run contents:");
        case NodeType.Shape:
            Shape childShape = (Shape)child;
            Console.WriteLine($"\t{childShape.ShapeType}, {childShape.Width}x{childShape.Height}");

See Also