/// <summary>Recursively clone the current element and all it's children and return a new navigator for the resulting subtree.</summary> /// <returns>A new <see cref="ElementDefinitionNavigator"/> instance that wraps the cloned element list.</returns> internal static ElementDefinitionNavigator CloneSubtree(this ElementDefinitionNavigator nav) { if (nav == null) { throw new ArgumentNullException(nameof(nav)); } if (nav.Current == null) { throw new ArgumentException(nameof(nav)); } var result = new ElementDefinitionNavigator(new ElementDefinition[] { (ElementDefinition)nav.Current.DeepCopy() }); result.MoveToFirstChild(); result.CopyChildren(nav); return(result); }
/// <summary> /// Insert the children of the source navigator under the node pointed to by this Navigator. /// </summary> /// <param name="dest"></param> /// <param name="source"></param> /// <returns></returns> public static bool CopyChildren(this ElementDefinitionNavigator dest, ElementDefinitionNavigator source) { if (dest.HasChildren) { return(false); // Protect children from being overwritten } if (!source.MoveToFirstChild()) { return(true); // Nothing to copy, but successful anyway } bool firstChild = true; do { var copiedChild = (ElementDefinition)source.Current.DeepCopy(); if (firstChild) { // The first time, create a new child in the destination dest.InsertFirstChild(copiedChild); firstChild = false; } else { // Then insert other childs after that dest.InsertAfter(copiedChild); } // If there are nested children in the source, insert them under // the newly inserted node in the destination if (source.HasChildren) { dest.CopyChildren(source); } }while (source.MoveToNext()); // Bring both source & destination back one step to the original parents source.MoveToParent(); dest.MoveToParent(); return(true); }