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: 19.9
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 methodContains
Determines whether a node is in the collection.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetEnumerator
Public methodGetHashCode
Serves as a hash function for a particular type.
(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 methodInsert
Inserts a node into the collection at the specified index.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRemove
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.)
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.
[C#]

Document doc = new Document(MyDir + "Shape.ReplaceTextboxesWithImages.doc");

// 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 + "Hammer.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