如何从Xml中删除单个节点 [英] how to remove a single node from Xml

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

问题描述

大家好,

我将XML文件作为我的数据库.我要插入到XML文件中的数据正在插入.

但是现在我想从该XML文件中删除一个节点.这是我的XML文件内容.

Hi every one,

I am taking XML file as my data base. The data that I want to insert into XML file it is getting inserted.

But now I want to remove a node from that XML file. Here is my XML file content.

<Cluster_list>
  <Cluster>A</Cluster>
  <Cluster>B</Cluster>
  <Cluster>C</Cluster>
</Cluster_list>


现在,我要从该列表中删除值为C的节点.

我写了这样的代码.


Now I want to remove the node which has the value C from that list.

I wrote code like this.

XmlDocument doc = new XmlDocument();
                doc.Load(cluster_path);//Path is given globly

 XmlNode node = doc.SelectSingleNode("/Cluster_list [Cluster='"+cluster_name+"']");

node.ParentNode.RemoveChild(node);


                doc.Save(cluster_path);


在这里,我将给出值cluster_name=C.

错误出现在-doc.Save(cluster_path);
说-"无效的xml文档,该文档没有根元素,但确实具有根元素"

我在互联网上搜索,但没有适当的解决方案.因此,任何机构都可以提出任何解决方案.

预先感谢
Arun


Here I will give the value cluster_name=C.

The error is coming at the - doc.Save(cluster_path);
Saying - "invalid xml document the document does not have a root element it does have a root element"

I searched in internet but I didn''t got proper solution. So can any body suggest any solution.

Advance Thanks
Arun

推荐答案

在LINQ to XML中切换为使用XElement ...使处理和操作XML比使用XmlDocument更容易. Web上有很多示例.
Switch to using XElement ... in LINQ to XML ... makes handling and manipulating XML much easier than using XmlDocument. Plenty of examples on the web.


我对C#到xml的工作还不多,但是您可以使用它:
I have not worked so much on c# to xml but u can use this :
XmlNode.RemoveChild(XmlNode);


n搜索有关此内容.这一定会对您有帮助. :)


n search about this. this will surely help you. :)


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

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