I have a simple function that's designed to copy a section of an xml document to another. I want to replace one node with the other so ReplaceChild seems like the logical choice. I keep getting the error 'The reference node is not a child of this node.' though. That seems odd since I found that node by asking for the parent in the first place. Any idea what I'm doing wrong?
    private static void KeepSection(XmlDocument newDoc, XmlDocument currentDoc, XmlNamespaceManager nsmgr, string path)
    {
        XmlNode section = currentDoc.SelectSingleNode(path, nsmgr);
        XmlNode newSection = newDoc.SelectSingleNode(path, nsmgr);
        if (newSection != null && section != null)
        {
            XmlNode parent = newSection.ParentNode;
            parent.ReplaceChild(newSection, newDoc.ImportNode(section, true));
        }
    }
It looks like you have your ReplaceChild parameters reversed:
public virtual XmlNode ReplaceChild(
    XmlNode newChild,
    XmlNode oldChild
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With