NodeCollection

NodeCollection class

Represents a collection of nodes of a specific type.

To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.

public class NodeCollection : IEnumerable<Node>

Properties

NameDescription
Count { get; }Gets the number of nodes in the collection.
Item { get; }Retrieves a node at the given index.

Methods

NameDescription
Add(Node)Adds a node to the end of the collection.
Clear()Removes all nodes from this collection and from the document.
Contains(Node)Determines whether a node is in the collection.
GetEnumerator()Provides a simple “foreach” style iteration over the collection of nodes.
IndexOf(Node)Returns the zero-based index of the specified node.
Insert(int, Node)Inserts a node into the collection at the specified index.
Remove(Node)Removes the node from the collection and from the document.
RemoveAt(int)Removes the node at the specified index from the collection and from the document.
ToArray()Copies all nodes from the collection to a new array of nodes.

Remarks

NodeCollection does not own the nodes it contains, rather, is just a selection of nodes of the specified type, but the nodes are stored in the tree under their respective parent nodes.

NodeCollection supports indexed access, iteration and provides add and remove methods.

The NodeCollection collection is “live”, i.e. changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the NodeCollection properties and methods.

NodeCollection is returned by GetChildNodes and also serves as a base class for typed node collections such as SectionCollection, ParagraphCollection etc.

NodeCollection can be “flat” and contain only immediate children of the node it was created from, or it can be “deep” and contain all descendant children.

Examples

Shows how to replace all textbox shapes with image shapes.

Document doc = new Document(MyDir + "Textboxes in drawing canvas.docx");

Shape[] shapes = doc.GetChildNodes(NodeType.Shape, true).OfType<Shape>().ToArray();

Assert.AreEqual(3, shapes.Count(s => s.ShapeType == ShapeType.TextBox));
Assert.AreEqual(1, shapes.Count(s => s.ShapeType == ShapeType.Image));

foreach (Shape shape in shapes)
{
    if (shape.ShapeType == ShapeType.TextBox)
    {
        Shape replacementShape = new Shape(doc, ShapeType.Image);
        replacementShape.ImageData.SetImage(ImageDir + "Logo.jpg");
        replacementShape.Left = shape.Left;
        replacementShape.Top = shape.Top;
        replacementShape.Width = shape.Width;
        replacementShape.Height = shape.Height;
        replacementShape.RelativeHorizontalPosition = shape.RelativeHorizontalPosition;
        replacementShape.RelativeVerticalPosition = shape.RelativeVerticalPosition;
        replacementShape.HorizontalAlignment = shape.HorizontalAlignment;
        replacementShape.VerticalAlignment = shape.VerticalAlignment;
        replacementShape.WrapType = shape.WrapType;
        replacementShape.WrapSide = shape.WrapSide;

        shape.ParentNode.InsertAfter(replacementShape, shape);
        shape.Remove();
    }
}

shapes = doc.GetChildNodes(NodeType.Shape, true).OfType<Shape>().ToArray();

Assert.AreEqual(0, shapes.Count(s => s.ShapeType == ShapeType.TextBox));
Assert.AreEqual(4, shapes.Count(s => s.ShapeType == ShapeType.Image));

doc.Save(ArtifactsDir + "Shape.ReplaceTextboxesWithImages.docx");

See Also