作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我需要根据条件删除 XmlNode。怎么做?
foreach (XmlNode drawNode in nodeList)
{
//Based on a condition
drawNode.RemoveAll(); //need to remove the entire node
}
最佳答案
这应该可以解决问题:
for (int i = nodeList.Count - 1; i >= 0; i--)
{
nodeList[i].ParentNode.RemoveChild(nodeList[i]);
}
如果您使用常规的 for 循环循环,并“向后”循环,您可以随时删除项目。
更新:这里是一个完整的例子,包括加载一个xml文件,定位节点,删除它们并保存文件:
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("some-xpath-query");
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(fileName);
关于c# - 如何从 XmlNodeList 中删除 XmlNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/875136/
我是一名优秀的程序员,十分优秀!