C# 从 XmlDocument 中删除节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing nodes from an XmlDocument
提问by FlySwat
The following code should find the appropriate project tag and remove it from the XmlDocument, however when I test it, it says:
下面的代码应该找到合适的项目标签并将其从 XmlDocument 中删除,但是当我测试它时,它说:
The node to be removed is not a child of this node.
要删除的节点不是该节点的子节点。
Does anyone know the proper way to do this?
有谁知道正确的方法来做到这一点?
public void DeleteProject (string projectName)
{
string ccConfigPath = ConfigurationManager.AppSettings["ConfigPath"];
XmlDocument configDoc = new XmlDocument();
configDoc.Load(ccConfigPath);
XmlNodeList projectNodes = configDoc.GetElementsByTagName("project");
for (int i = 0; i < projectNodes.Count; i++)
{
if (projectNodes[i].Attributes["name"] != null)
{
if (projectName == projectNodes[i].Attributes["name"].InnerText)
{
configDoc.RemoveChild(projectNodes[i]);
configDoc.Save(ccConfigPath);
}
}
}
}
UPDATE
更新
Fixed. I did two things:
固定的。我做了两件事:
XmlNode project = configDoc.SelectSingleNode("//project[@name='" + projectName + "']");
Replaced the For loop with an XPath query, which wasn't for fixing it, just because it was a better approach.
用 XPath 查询替换了 For 循环,这不是为了修复它,只是因为它是一种更好的方法。
The actual fix was:
实际的修复是:
project.ParentNode.RemoveChild(project);
Thanks Pat and Chuck for this suggestion.
感谢帕特和查克的这个建议。
采纳答案by Pat
Instead of
代替
configDoc.RemoveChild(projectNodes[i]);
try
尝试
projectNodes[i].parentNode.RemoveChild(projectNodes[i]);
回答by Greg Hurlman
Is it possible that the project nodes aren't child nodes, but grandchildren or lower? GetElementsByTagName will give you elements from anywhere in the child element tree, IIRC.
项目节点是否可能不是子节点,而是孙子节点或更低?GetElementsByTagName 将为您提供来自子元素树 IIRC 中任何位置的元素。
回答by David Hayes
It would be handy to see a sample of the XML file you're processing but my guess would be that you have something like this
查看您正在处理的 XML 文件的示例会很方便,但我猜您有这样的内容
<Root>
<Blah>
<project>...</project>
</Blah>
</Root>
The error message seems to be because you're trying to remove <project>
from the grandparent rather than the direct parent of the project node
错误消息似乎是因为您试图<project>
从祖父节点而不是项目节点的直接父节点中删除
回答by Jason
try
尝试
configDoc.DocumentElement.RemoveChild(projectNodes[i]);
回答by Chuck
Looks like you need to select the parent node of projectNodes[i] before calling RemoveChild.
看起来您需要在调用 RemoveChild 之前选择 projectNodes[i] 的父节点。
回答by Eugene Ryabtsev
When you get sufficiently annoyed by writing it the long way (for me that was fairly soon) you can use a helper extension method provided below. Yay new technology!
当你写得很长很烦时(对我来说这很快),你可以使用下面提供的辅助扩展方法。耶新技术!
public static class Extensions {
...
public static XmlNode RemoveFromParent(this XmlNode node) {
return (node == null) ? null : node.ParentNode.RemoveChild(node);
}
}
...
//some_long_node_expression.parentNode.RemoveChild(some_long_node_expression);
some_long_node_expression.RemoveFromParent();