CompositeNodeCount Property |
Namespace: Aspose.Words
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. "); paragraph.AppendChild(paragraphText); // 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. " + (char) 12, paragraph.GetText()); // 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 paragraph.PrependChild(run1); Assert.AreEqual("Run 1. Run 2. Initial text. Run 3. " + (char) 12, paragraph.GetText()); 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. " + (char) 12, paragraph.GetText()); Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);