NodeCollection Class

Represents a collection of nodes of a specific type.
Inheritance Hierarchy
SystemObject
  Aspose.WordsNodeCollection
    More...

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class NodeCollection : IEnumerable<Node>, 
	IEnumerable

The NodeCollection type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleCount
Gets the number of nodes in the collection.
Public propertyCode exampleItem
Retrieves a node at the given index.
Methods
  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 (Inherited from Object.)
Public methodGetEnumerator
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (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 (Inherited from Object.)
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(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.

Examples
Shows how to replace all textboxes with images.
Document doc = new Document(MyDir + "Textboxes in drawing canvas.docx");

// This gets a live collection of all shape nodes in the document
NodeCollection shapeCollection = doc.GetChildNodes(NodeType.Shape, true);

// Since we will be adding/removing nodes, it is better to copy all collection
// into a fixed size array, otherwise iterator will be invalidated
Node[] shapes = shapeCollection.ToArray();

foreach (Shape shape in shapes.OfType<Shape>())
{
    // Filter out all shapes that we don't need
    if (shape.ShapeType.Equals(ShapeType.TextBox))
    {
        // Create a new shape that will replace the existing shape
        Shape image = new Shape(doc, ShapeType.Image);

        // Load the image into the new shape
        image.ImageData.SetImage(ImageDir + "Windows MetaFile.wmf");

        // Make new shape's position to match the old shape
        image.Left = shape.Left;
        image.Top = shape.Top;
        image.Width = shape.Width;
        image.Height = shape.Height;
        image.RelativeHorizontalPosition = shape.RelativeHorizontalPosition;
        image.RelativeVerticalPosition = shape.RelativeVerticalPosition;
        image.HorizontalAlignment = shape.HorizontalAlignment;
        image.VerticalAlignment = shape.VerticalAlignment;
        image.WrapType = shape.WrapType;
        image.WrapSide = shape.WrapSide;

        // Insert new shape after the old shape and remove the old shape
        shape.ParentNode.InsertAfter(image, shape);
        shape.Remove();
    }
}

doc.Save(ArtifactsDir + "Shape.ReplaceTextboxesWithImages.doc");
See Also
Inheritance Hierarchy