CompositeNode class

Base class for nodes that can contain other nodes.

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

public abstract class CompositeNode : Node, IEnumerable<Node>, IXPathNavigable


Name Description
ChildNodes { get; } Gets all immediate child nodes of this node.
Count { get; } Gets the number of immediate children of this node.
CustomNodeId { get; set; } Specifies custom node identifier.
virtual Document { get; } Gets the document to which this node belongs.
FirstChild { get; } Gets the first child of the node.
HasChildNodes { get; } Returns true if this node has any child nodes.
override IsComposite { get; } Returns true as this node can have child nodes.
LastChild { get; } Gets the last child of the node.
NextSibling { get; } Gets the node immediately following this node.
abstract NodeType { get; } Gets the type of this node.
ParentNode { get; } Gets the immediate parent of this node.
PreviousSibling { get; } Gets the node immediately preceding this node.
Range { get; } Returns a Range object that represents the portion of a document that is contained in this node.


Name Description
abstract Accept(DocumentVisitor) Accepts a visitor.
AppendChild(Node) Adds the specified node to the end of the list of child nodes for this node.
Clone(bool) Creates a duplicate of the node.
CreateNavigator() Reserved for system use. IXPathNavigable.
GetAncestor(NodeType) Gets the first ancestor of the specified NodeType.
GetAncestor(Type) Gets the first ancestor of the specified object type.
GetChild(NodeType, int, bool) Returns an Nth child node that matches the specified type.
GetChildNodes(NodeType, bool) Returns a live collection of child nodes that match the specified type.
GetEnumerator() Provides support for the for each style iteration over the child nodes of this node.
override GetText() Gets the text of this node and of all its children.
IndexOf(Node) Returns the index of the specified child node in the child node array.
InsertAfter(Node, Node) Inserts the specified node immediately after the specified reference node.
InsertBefore(Node, Node) Inserts the specified node immediately before the specified reference node.
NextPreOrder(Node) Gets next node according to the pre-order tree traversal algorithm.
PrependChild(Node) Adds the specified node to the beginning of the list of child nodes for this node.
PreviousPreOrder(Node) Gets the previous node according to the pre-order tree traversal algorithm.
Remove() Removes itself from the parent.
RemoveAllChildren() Removes all the child nodes of the current node.
RemoveChild(Node) Removes the specified child node.
RemoveSmartTags() Removes all SmartTag descendant nodes of the current node.
SelectNodes(string) Selects a list of nodes matching the XPath expression.
SelectSingleNode(string) Selects the first Node that matches the XPath expression.
ToString(SaveFormat) Exports the content of the node into a string in the specified format.
ToString(SaveOptions) Exports the content of the node into a string using the specified save options.


A document is represented as a tree of nodes, similar to DOM or XmlDocument.

For more info see the Composite design pattern.

The CompositeNode class:

  • Provides access to the child nodes.
  • Implements Composite operations such as insert and remove children.
  • Provides methods for XPath navigation.


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