CompositeNodeCreateNavigator Method
Reserved for system use. IXPathNavigable.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public XPathNavigator CreateNavigator()

Return Value

Type: XPathNavigator

Implements

IXPathNavigableCreateNavigator
Examples
Shows how to create an XPathNavigator and use it to traverse and read nodes.
public void NodeXPathNavigator()
{
    // Create a blank document
    Document doc = new Document();

    // A document is a composite node so we can make a navigator straight away
    XPathNavigator navigator = doc.CreateNavigator();

    // Our root is the document node with 1 child, which is the first section
    if (navigator != null)
    {
        Assert.AreEqual("Document", navigator.Name);
        Assert.AreEqual(false, navigator.MoveToNext());
        Assert.AreEqual(1, navigator.SelectChildren(XPathNodeType.All).Count);

        // The document tree has the document, first section, body and first paragraph as nodes, with each being an only child of the previous
        // We can add a few more to give the tree some branches for the navigator to traverse
        DocumentBuilder docBuilder = new DocumentBuilder(doc);
        docBuilder.Write("Section 1, Paragraph 1. ");
        docBuilder.InsertParagraph();
        docBuilder.Write("Section 1, Paragraph 2. ");
        doc.AppendChild(new Section(doc));
        docBuilder.MoveToSection(1);
        docBuilder.Write("Section 2, Paragraph 1. ");

        // Use our navigator to print a map of all the nodes in the document to the console
        StringBuilder stringBuilder = new StringBuilder();
        MapDocument(navigator, stringBuilder, 0);
        Console.Write(stringBuilder.ToString());
    }
}

/// <summary>
/// This will traverse all children of a composite node and map the structure in the style of a directory tree.
/// Amount of space indentation indicates depth relative to initial node. Only runs will have their values printed.
/// </summary>
private void MapDocument(XPathNavigator navigator, StringBuilder stringBuilder, int depth)
{
    do
    {
        stringBuilder.Append(' ', depth);
        stringBuilder.Append(navigator.Name + ": ");

        if (navigator.Name == "Run")
        {
            stringBuilder.Append(navigator.Value);
        }

        stringBuilder.Append('\n');

        if (navigator.HasChildren)
        {
            navigator.MoveToFirstChild();
            MapDocument(navigator, stringBuilder, depth + 1);
            navigator.MoveToParent();
        }
    } while (navigator.MoveToNext());
}
See Also