gpt4 book ai didi

c# - 函数 RemoveChild(XmlNode 节点)在 C# 中失败

转载 作者:数据小太阳 更新时间:2023-10-29 02:15:07 24 4
gpt4 key购买 nike

当我尝试使用 RemoveChild() 删除我的一些子元素时。但是抛出异常。我在下面附上了我的代码。

    nodeName = doc.SelectSingleNode("//Equipment//DataCollections//EnabledIDs//MyID[@id='" + attrValue + "']"); 
// Found the nodeName successfully druing run time.
doc.DocumentElement.RemoveChild(nodeName);
// faild to Remove the node

在下面显示错误:

An unhandled exception of type 'System.ArgumentException' occurred in System.Xml.dll

Additional information: The node to be removed is not a child of this node.

如何删除节点?

[更新]

使用 VS2005 和 .NET 2.0。

最佳答案

我相信 .RemoveChild 正在删除您选择的节点的子节点。

nodeName 下是否有子节点或者已经是叶子节点了?

编辑:

实际上您需要删除父级的子级,请尝试以下操作:

nodeName.parentNode.removeChild(nodeName)

关于c# - 函数 RemoveChild(XmlNode 节点)在 C# 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com