We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

What is "pre-order algorithm"

The technical document for the Node.NextPreOrder method says “Gets next node according to the pre-order tree traversal algorithm”
What, exactly, is the “pre-order tree traversal algorithm”?
I am writing a method to delete all everything between two “bookend” bookmarks (“XXXStart” and “XXXEnd”). I’m taking the first BookmarkStart node, going up it’s ParentNode tree to find the enclosing Section node and calling that “topNode”, because I don’t want to go outside the containing section. I then used a while loop similar to that in the example for deleting all images. But I’m finding that, in some cases, Node.NextPreorder is returning Null after deleting a few paragraphs.
Here’s the code:

topNode = bkMark.ParentNode
While Not topNode Is Nothing AndAlso (topNode.NodeType <> NodeType.Body And topNode.NodeType <> NodeType.Section)
topNode = topNode.ParentNode
End While
docNode = bkMark.NextPreOrder(topNode)
While Not docNode Is Nothing
If docNode.NodeType = NodeType.BookmarkStart Then
endingBkMark = docNode
If endingBkMark.Bookmark.Name = endBkMark Then
'Then we've marked all the runs between starting bookmark and ending bookmark
Exit While
End If
End If
thisNode = docNode
docNode = docNode.NextPreOrder(topNode)
End While

Any hints as to why this is not working?

Thanks for your inquiry. Please try using code provided in the following thread.
Best regards.

Thanks for your reponse. That code does seem to work. Except…
And this problem is not particularly related to that code.
The documents I’m working with have a large number of blocks of text marked by the type of bookends that I described. Many of these blocks butt right up against one another, so that the ending bookmark of one is co-located with the starting bookmark of another. When that happens, it seems like the order they appear in the XML file is random. I.E, sometimes, but not always, I see code like this in the XML:

<w:bookmarkStart w:id="65" w:name="ShowViewStart" />
<w:bookmarkStart w:id="66" w:name="ShowEscEnd" />
<w:bookmarkEnd w:id="65" />
<w:bookmarkEnd w:id="66" />

In this case, when I delete the “ShowEsc” block which appears before this code, I’m also going to take out the Starting bookmark for the “ShowView” block ,which follows this code. Then, when I go to process the ShowView block, it’s going to fail because the ShowViewStart bookmark is missing.
Can you think of a way to handle this, preferably without modifying the templates. There are a large number of templates (Word 2003 compatible) which are stored in binary form in a database, and extracted as required. Modifying them will be a pain. But more problematic is that I cannot think of a way to modify them that would guarantee that the bookmarks are stored in a non-overlapping fashion.
I do realize that this not specifically an Aspose problem, but more with the logic of what we’re trying to do. Thanks for your help.

Thanks for your inquiry. I think you can exclude BookmarkStart and BookmarkEnd nodes from the deleting process. You can use logic like the following.

if (currNode.NodeType != NodeType.BookmarkStart && currNode.NodeType != NodeType.BookmarkEnd)
    // Remove node

Hope this helps.
Also please attach your document for testing.
Best regards.

Did this one slip through the cracks? I have the same question…
What exactly is “pre-order tree traversal algorithm?”


Thanks for your inquiry. Please see the following link to learn more about tree traversal algorithm.
Best regards.