NodeParentNode Property

Gets the immediate parent of this node.

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

ExpandedSyntax

public CompositeNode ParentNode { get; }

Property Value

Type: CompositeNode

ExpandedRemarks

If a node has just been created and not yet added to the tree, or if it has been removed from the tree, the parent is null.

ExpandedExamples

Shows how to access the parent node.
Document doc = new Document();

// Get the document's first paragraph and append a child node to it in the form of a run with text
Paragraph para = doc.FirstSection.Body.FirstParagraph;

// When inserting a new node, the document that the node will belong to must be provided as an argument
Run run = new Run(doc, "Hello world!");
para.AppendChild(run);

// The node lineage can be traced back to the document itself
Assert.AreEqual(para, run.ParentNode);
Assert.AreEqual(doc.FirstSection.Body, para.ParentNode);
Assert.AreEqual(doc.FirstSection, doc.FirstSection.Body.ParentNode);
Assert.AreEqual(doc, doc.FirstSection.ParentNode);
Shows how to create a node and set its owning document.
// Open a file from disk
Document doc = new Document();

// Creating a new node of any type requires a document passed into the constructor
Paragraph para = new Paragraph(doc);

// The new paragraph node does not yet have a parent
Console.WriteLine("Paragraph has no parent node: " + (para.ParentNode == null));

// But the paragraph node knows its document
Console.WriteLine("Both nodes' documents are the same: " + (para.Document == doc));

// The fact that a node always belongs to a document allows us to access and modify 
// properties that reference the document-wide data such as styles or lists
para.ParagraphFormat.StyleName = "Heading 1";

// Now add the paragraph to the main text of the first section
doc.FirstSection.Body.AppendChild(para);

// The paragraph node is now a child of the Body node
Console.WriteLine("Paragraph has a parent node: " + (para.ParentNode != null));

ExpandedSee Also