NodeCollection Class

Represents a collection of nodes of a specific type.

ExpandedInheritance Hierarchy

SystemObject
  Aspose.WordsNodeCollection
    More...

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 21.12.0

ExpandedSyntax

public class NodeCollection : IEnumerable<Node>, 
	IEnumerable

The NodeCollection type exposes the following members.

ExpandedProperties

  NameDescription
Public propertyCode exampleCount
Gets the number of nodes in the collection.
Public propertyCode exampleItem
Retrieves a node at the given index.

ExpandedMethods

  NameDescription
Public methodCode exampleAdd
Adds a node to the end of the collection.
Public methodCode exampleClear
Removes all nodes from this collection and from the document.
Public methodCode exampleContains
Determines whether a node is in the collection.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodGetEnumerator
Provides a simple "foreach" style iteration over the collection of nodes.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodCode exampleIndexOf
Returns the zero-based index of the specified node.
Public methodCode exampleInsert
Inserts a node into the collection at the specified index.
Public methodCode exampleRemove
Removes the node from the collection and from the document.
Public methodCode exampleRemoveAt
Removes the node at the specified index from the collection and from the document.
Public methodCode exampleToArray
Copies all nodes from the collection to a new array of nodes.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)

ExpandedRemarks

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(NodeType, Boolean) 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.

ExpandedExamples

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");

ExpandedSee Also

ExpandedInheritance Hierarchy