private void CopyNode(XPathNavigator navigator) {
XPathNodeType nodeType;
int depthStart = this.depth;
Debug.Assert(navigator != null);
while (true) {
if (StartCopy(navigator, this.depth == depthStart)) {
nodeType = navigator.NodeType;
Debug.Assert(nodeType == XPathNodeType.Element, "StartCopy should return true only for Element nodes.");
// Copy attributes
if (navigator.MoveToFirstAttribute()) {
do {
StartCopy(navigator, false);
}
while (navigator.MoveToNextAttribute());
navigator.MoveToParent();
}
// Copy namespaces in document order (navigator returns them in reverse document order)
CopyNamespaces(navigator, (this.depth - 1 == depthStart) ? XPathNamespaceScope.ExcludeXml : XPathNamespaceScope.Local);
StartElementContentUnchecked();
// If children exist, move down to next level
if (navigator.MoveToFirstChild())
continue;
EndCopy(navigator, (this.depth - 1) == depthStart);
}
// No children
while (true) {
if (this.depth == depthStart) {
// The entire subtree has been copied
return;
}
if (navigator.MoveToNext()) {
// Found a sibling, so break to outer loop
break;
}
// No siblings, so move up to previous level
navigator.MoveToParent();
EndCopy(navigator, (this.depth - 1) == depthStart);
}
}
}