diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs
index 9ac347c4..b4dfce77 100644
--- a/src/HtmlAgilityPack.Shared/HtmlNode.cs
+++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs
@@ -979,6 +979,21 @@ public HtmlNode AppendChild(HtmlNode newChild)
_ownerdocument.SetIdForNode(newChild, newChild.GetId());
SetChildNodesId(newChild);
+ var parentnode = _parentnode;
+ HtmlDocument lastOwnerDocument = null;
+ while (parentnode != null)
+ {
+ if(parentnode.OwnerDocument != lastOwnerDocument)
+ {
+ parentnode.OwnerDocument.SetIdForNode(newChild, newChild.GetId());
+ parentnode.SetChildNodesId(newChild);
+ lastOwnerDocument = parentnode.OwnerDocument;
+ }
+
+ parentnode = parentnode._parentnode;
+ }
+
+
SetChanged();
return newChild;
}