删除所有XML文件节点 [英] Delete all XML file Nodes

查看:71
本文介绍了删除所有XML文件节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除XML文件的所有节点?

我尝试了这段代码.但是给出了堆栈溢出错误.

How can i delete all nodes of a XML file?

i tried this code.But gives an stack overflow error.

XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.SelectNodes("//Setting[@name=''File1'']");
for (int i = nodes.Count - 1; i >= 0; i--)
{
    nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(path);

推荐答案

不完全确定您要实现的目标...您可以调用doc.RemoveAll()删除除根节点(和属性)以外的所有内容以及).如果您需要删除所有内容,为什么不只按路径删除/覆盖文件呢?
Not exactly sure what are you trying to achieve... you could just call doc.RemoveAll() to remove all but root node (and attributes as well). If you need to delete everything, why not just delete/overwrite a file by path?


这是要执行的操作:

  • 使用问题中显示的所有代码,然后...
  • 致电:doc = new XmlDocument();
Here is what to do:

  • Use all your code shown in your question, then…
  • Call: doc = new XmlDocument();


这篇关于删除所有XML文件节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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