NodeNodeTypeToString Method

A utility method that converts a node type enum value into a user friendly string.

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

Syntax

public static string NodeTypeToString(
	NodeType nodeType
)

Parameters

nodeType
Type: Aspose.WordsNodeType

Return Value

Type: String

Examples

Shows how to enumerate immediate child nodes of a composite node using NextSibling.
Document doc = new Document(MyDir + "Paragraphs.docx");

// Loop starting from the first child until we reach null
for (Node node = doc.FirstSection.Body.FirstChild; node != null; node = node.NextSibling)
{
    // Output the types of the nodes that we come across
    Console.WriteLine(Node.NodeTypeToString(node.NodeType));
}
Shows how to efficiently visit all direct and indirect children of a composite node.
public void RecurseAllNodes()
{
    Document doc = new Document(MyDir + "Paragraphs.docx");

    // Any node that can contain child nodes, such as the document itself, is composite
    Assert.True(doc.IsComposite);

    // Invoke the recursive function that will go through and print all the child nodes of a composite node
    TraverseAllNodes(doc, 0);
}

/// <summary>
/// Recursively traverses a node tree while printing the type of each node with an indent depending on depth as well as the contents of all inline nodes.
/// </summary>
public void TraverseAllNodes(CompositeNode parentNode, int depth)
{
    // Loop through immediate children of a node
    for (Node childNode = parentNode.FirstChild; childNode != null; childNode = childNode.NextSibling)
    {
        Console.Write($"{new string('\t', depth)}{Node.NodeTypeToString(childNode.NodeType)}");

        // Recurse into the node if it is a composite node
        if (childNode.IsComposite)
        {
            Console.WriteLine();
            TraverseAllNodes((CompositeNode)childNode, depth + 1);
        }
        else if (childNode is Inline)
        {
            Console.WriteLine($" - \"{childNode.GetText().Trim()}\"");
        }
        else
        {
            Console.WriteLine();
        }
    }
}

See Also