Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlNode.ReplaceChild is complaining that the node I'm trying to remove is not a child despite the fact that I got to the node via ParentNode

Tags:

c#

xml

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));
        }
    }
like image 734
Colin Newell Avatar asked Dec 18 '09 21:12

Colin Newell


1 Answers

It looks like you have your ReplaceChild parameters reversed:

public virtual XmlNode ReplaceChild(
    XmlNode newChild,
    XmlNode oldChild
)
like image 67
Andy West Avatar answered Nov 15 '22 05:11

Andy West