Aspose::Words::Node::get_ParentNode method
Contents
[
Hide
]Node::get_ParentNode method
Gets the immediate parent of this node.
System::SharedPtr<Aspose::Words::CompositeNode> Aspose::Words::Node::get_ParentNode()
Remarks
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.
Examples
Shows how to access a node’s parent node.
auto doc = System::MakeObject<Aspose::Words::Document>();
System::SharedPtr<Aspose::Words::Paragraph> para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
// Append a child Run node to the document's first paragraph.
auto run = System::MakeObject<Aspose::Words::Run>(doc, u"Hello world!");
para->AppendChild<System::SharedPtr<Aspose::Words::Run>>(run);
// The paragraph is the parent node of the run node. We can trace this lineage
// all the way to the document node, which is the root of the document's node tree.
ASPOSE_ASSERT_EQ(run->get_ParentNode(), para);
ASPOSE_ASSERT_EQ(para->get_ParentNode(), doc->get_FirstSection()->get_Body());
ASPOSE_ASSERT_EQ(doc->get_FirstSection()->get_Body()->get_ParentNode(), doc->get_FirstSection());
ASPOSE_ASSERT_EQ(doc->get_FirstSection()->get_ParentNode(), doc);
Shows how to create a node and set its owning document.
auto doc = System::MakeObject<Aspose::Words::Document>();
auto para = System::MakeObject<Aspose::Words::Paragraph>(doc);
para->AppendChild<System::SharedPtr<Aspose::Words::Run>>(System::MakeObject<Aspose::Words::Run>(doc, u"Hello world!"));
// We have not yet appended this paragraph as a child to any composite node.
ASSERT_TRUE(System::TestTools::IsNull(para->get_ParentNode()));
// If a node is an appropriate child node type of another composite node,
// we can attach it as a child only if both nodes have the same owner document.
// The owner document is the document we passed to the node's constructor.
// We have not attached this paragraph to the document, so the document does not contain its text.
ASPOSE_ASSERT_EQ(doc, para->get_Document());
ASSERT_EQ(doc->GetText().Trim(), System::String::Empty);
// Since the document owns this paragraph, we can apply one of its styles to the paragraph's contents.
para->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 1"));
// Add this node to the document, and then verify its contents.
doc->get_FirstSection()->get_Body()->AppendChild<System::SharedPtr<Aspose::Words::Paragraph>>(para);
ASPOSE_ASSERT_EQ(para->get_ParentNode(), doc->get_FirstSection()->get_Body());
ASSERT_EQ(doc->GetText().Trim(), u"Hello world!");
See Also
- Class Node
- Namespace Aspose::Words
- Library Aspose.Words for C++