CompositeNodePrependChild Method

Adds the specified node to the beginning of the list of child nodes for this node.

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


public Node PrependChild(
	Node newChild


Type: Aspose.WordsNode
The node to add.

Return Value

Type: Node
The node added.


If the newChild is already in the tree, it is first removed.

If the node being inserted was created from another document, you should use ImportNode(Node, Boolean, ImportFormatMode) to import the node to the current document. The imported node can then be inserted into the current document.


Shows how to add, update and delete child nodes from a CompositeNode's child collection.
Document doc = new Document();

// An empty document has one paragraph by default
Assert.AreEqual(1, doc.FirstSection.Body.Paragraphs.Count);

// A paragraph is a composite node because it can contain runs, which are another type of node
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Run paragraphText = new Run(doc, "Initial text. ");

// We will place these 3 children into the main text of our paragraph
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "Run 3. ");

// We initialized them but not in our paragraph yet
Assert.AreEqual("Initial text.", paragraph.GetText().Trim());

// Insert run2 before initial paragraph text. This will be at the start of the paragraph
paragraph.InsertBefore(run2, paragraphText);

// Insert run3 after initial paragraph text. This will be at the end of the paragraph
paragraph.InsertAfter(run3, paragraphText);

// Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1

Assert.AreEqual("Run 1. Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(4, paragraph.GetChildNodes(NodeType.Any, true).Count);

// Access the child node collection and update/delete children
((Run)paragraph.GetChildNodes(NodeType.Run, true)[1]).Text = "Updated run 2. ";
paragraph.GetChildNodes(NodeType.Run, true).Remove(paragraphText);

Assert.AreEqual("Run 1. Updated run 2. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);

See Also