Node Class
Base class for all nodes of a Word document.
Inheritance Hierarchy
SystemObject
  Aspose.WordsNode
    More...

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 17.6
Syntax
public abstract class Node

public abstract class Node

The Node type exposes the following members.

Constructors
  NameDescription
Protected methodNode
Initializes a new instance of the Node class
Protected methodNode(DocumentBase)
Initializes a new instance of the Node class
Top
Properties
  NameDescription
Public propertyCode exampleDocument
Gets the document to which this node belongs.
Public propertyCode exampleIsComposite
Returns true if this node can contain other nodes.
Public propertyCode exampleNextSibling
Gets the node immediately following this node.
Public propertyCode exampleNodeType
Gets the type of this node.
Public propertyCode exampleParentNode
Gets the immediate parent of this node.
Public propertyCode examplePreviousSibling
Gets the node immediately preceding this node.
Public propertyCode exampleRange
Returns a Range object that represents the portion of a document that is contained in this node.
Top
Methods
  NameDescription
Public methodCode exampleAccept
Accepts a visitor.
Public methodCode exampleClone
Creates a duplicate of the node.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetAncestor(Type)
Gets the first ancestor of the specified object type.
Public methodCode exampleGetAncestor(NodeType)
Gets the first ancestor of the specified NodeType.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodCode exampleGetText
Gets the text of this node and of all its children.
Public methodGetType
Gets the type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleNextPreOrder
Gets next node according to the pre-order tree traversal algorithm.
Public methodStatic memberCode exampleNodeTypeToString
A utility method that converts a node type enum value into a user friendly string.
Public methodPreviousPreOrder
Gets the previous node according to the pre-order tree traversal algorithm.
Public methodCode exampleRemove
Removes itself from the parent.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodCode exampleToString(SaveFormat)
Exports the content of the node into a string in the specified format.
Public methodCode exampleToString(SaveOptions)
Exports the content of the node into a string using the specified save options.
Public methodToTxt Obsolete.
Exports the content of the node into a string in plain text format.
Protected methodStatic memberVisitorActionToBool
Top
Remarks

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

For more info see the Composite design pattern.

The Node class:

  • Defines the child node interface.
  • Defines the interface for visiting nodes.
  • Provides default cloning capability.
  • Implements parent node and owner document mechanisms.
  • Implements access to sibling nodes.
Examples
Shows how to extract a specific child node from a CompositeNode by using the GetChild method and passing the NodeType and index.
[C#]

Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);
[Visual Basic]

Dim paragraph As Paragraph = CType(doc.GetChild(NodeType.Paragraph, 0, True), Paragraph)
Examples
Shows how to clone composite nodes with and without their child nodes.
[C#]

// Create a new empty document.
Document doc = new Document();

// Add some text to the first paragraph
Paragraph para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(new Run(doc, "Some text"));

// Clone the paragraph and the child nodes.
Node cloneWithChildren = para.Clone(true);
// Only clone the paragraph and no child nodes.
Node cloneWithoutChildren = para.Clone(false);
[Visual Basic]

' Create a new empty document.
Dim doc As New Document()

' Add some text to the first paragraph
Dim para As Paragraph = doc.FirstSection.Body.FirstParagraph
para.AppendChild(New Run(doc, "Some text"))

' Clone the paragraph and the child nodes.
Dim cloneWithChildren As Node = para.Clone(True)
' Only clone the paragraph and no child nodes.
Dim cloneWithoutChildren As Node = para.Clone(False)
Examples
Shows how to remove all nodes of a specific type from a composite node. In this example we remove tables from a section body.
[C#]

// Get the section that we want to work on.
Section section = doc.Sections[0];
Body body = section.Body;

// Select the first child node in the body.
Node curNode = body.FirstChild;

while (curNode != null)
{
    // Save the pointer to the next sibling node because if the current 
    // node is removed from the parent in the next step, we will have 
    // no way of finding the next node to continue the loop.
    Node nextNode = curNode.NextSibling;

    // A section body can contain Paragraph and Table nodes.
    // If the node is a Table, remove it from the parent.
    if (curNode.NodeType.Equals(NodeType.Table))
        curNode.Remove();

    // Continue going through child nodes until null (no more siblings) is reached.
    curNode = nextNode;
}
[Visual Basic]

' Get the section that we want to work on.
Dim section As Section = doc.Sections(0)
Dim body As Body = section.Body

' Select the first child node in the body.
Dim curNode As Node = body.FirstChild

Do While curNode IsNot Nothing
    ' Save the pointer to the next sibling node because if the current 
    ' node is removed from the parent in the next step, we will have 
    ' no way of finding the next node to continue the loop.
    Dim nextNode As Node = curNode.NextSibling

    ' A section body can contain Paragraph and Table nodes.
    ' If the node is a Table, remove it from the parent.
    If curNode.NodeType.Equals(NodeType.Table) Then
        curNode.Remove()
    End If

    ' Continue going through child nodes until null (no more siblings) is reached.
    curNode = nextNode
Loop
See Also
Inheritance Hierarchy