XmlNode.ReplaceChild 抱怨我试图删除的节点不是子节点,尽管我是通过 ParentNode 到达节点的 [英] 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

查看:15
本文介绍了XmlNode.ReplaceChild 抱怨我试图删除的节点不是子节点,尽管我是通过 ParentNode 到达节点的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的函数,旨在将 xml 文档的一部分复制到另一个部分.我想用另一个替换一个节点,所以 ReplaceChild 似乎是合乎逻辑的选择.我不断收到错误参考节点不是此节点的子节点".尽管.这看起来很奇怪,因为我首先通过询问父节点来找到该节点.知道我做错了什么吗?

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));
        }
    }

推荐答案

看起来你的 ReplaceChild 参数颠倒了:

It looks like you have your ReplaceChild parameters reversed:

public virtual XmlNode ReplaceChild(
    XmlNode newChild,
    XmlNode oldChild
)

这篇关于XmlNode.ReplaceChild 抱怨我试图删除的节点不是子节点,尽管我是通过 ParentNode 到达节点的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆