private XmlNode NextElemInPreOrder( XmlNode curNode ) {
Debug.Assert( curNode != null );
//For preorder walking, first try its child
XmlNode retNode = curNode.FirstChild;
if ( retNode == null ) {
//if no child, the next node forward will the be the NextSibling of the first ancestor which has NextSibling
//so, first while-loop find out such an ancestor (until no more ancestor or the ancestor is the rootNode
retNode = curNode;
while ( retNode != null
&& retNode != rootNode
&& retNode.NextSibling == null ) {
retNode = retNode.ParentNode;
}
//then if such ancestor exists, set the retNode to its NextSibling
if ( retNode != null && retNode != rootNode )
retNode = retNode.NextSibling;
}
if ( retNode == this.rootNode )
retNode = null;
return retNode;
}