/// <summary>Replace <paramref name="existingChild"/> with <paramref name="newChild"/>, using the same role and preserving the ordering of children.</summary>
        public void ReplaceChild(IElementTreeNode existingChild, IElementTreeNode newChild)
        {
            if (newChild != null)
            {
                ChildRole existingRole = RoleFor(existingChild);
                ChildrenAndRoles.Remove(existingChild);
                existingChild.Parent = null;
                AddChildWithRole(newChild, existingRole);
                UpdateChildOrderingReferences(existingChild, newChild);
            }
            else
            {
                RemoveChild(existingChild);
            }

            // Update the ChildOrdering references from oldReference to newReference
            void UpdateChildOrderingReferences(IElementTreeNode oldReference, IElementTreeNode newReference)
            {
                foreach (ChildOrdering eachChildOrdering in ChildOrderings)
                {
                    if (eachChildOrdering.Before == existingChild)
                    {
                        eachChildOrdering.Before = newChild;
                    }
                    if (eachChildOrdering.After == existingChild)
                    {
                        eachChildOrdering.After = newChild;
                    }
                }
            }
        }
 /// <summary>Return the immediate children of this having the supplied <paramref name="role"/>.</summary>
 private protected IEnumerable <IElementTreeNode> ChildrenWithRole(ChildRole role) => ChildrenAndRoles
 .Where(kvp => kvp.Value == role)
 .Select(kvp => kvp.Key);
 /// <summary>Find all children with assigned role <paramref name="originalRole"/>, and change their role to <paramref name="newRole"/>.</summary>
 private protected void ChangeChildRoles(ChildRole originalRole, ChildRole newRole) => ChildrenWithRole(originalRole).ToList()
 .ForEach(childToChange => SetRoleOfChild(childToChange, newRole));
 /// <summary>Change the role of an existing child <paramref name="child"/> to <paramref name="newRole"/>.</summary>
 public void SetRoleOfChild(IElementTreeNode child, ChildRole newRole) => ChildrenAndRoles[child] = newRole;
 /// <summary>Add all the ElementBuilders in <paramref name="children"/> as children of this, with ChildRole <paramref name="role"/>.</summary>
 private protected void AddChildrenWithRole(IEnumerable <IElementTreeNode> newChildren, ChildRole role) => newChildren.ToList().ForEach(newChild => AddChildWithRole(newChild, role));
 /// <summary>Add <paramref name="child"/> as a child of this, with ChildRole <paramref name="role"/></summary>
 public void AddChildWithRole(IElementTreeNode child, ChildRole role)
 {
     ChildrenAndRoles.Add(child, role);
     child.Parent = this;
 }