删除节点Linq到xml不起作用?! [英] Remove a node Linq to xml is not working?!

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

问题描述



我正在尝试删除节点。当我执行messagebox.show()时,它显示已找到该节点但文件中没有删除任何内容。我不确定我做错了什么。



Hi,
I am trying to remove a node. When I do a messagebox.show() it shows the node has been found but nothing is deleted in the file. I am not sure what I am doing wrong.

private static void RemoveElements()
        {
            XDocument doc = XDocument.Load("File.xml");

            var Result = (from c in doc.Descendants("Book")
                          where c.Attribute("ID").Value == "556544"
                          select c).First();
            MessageBox.Show(Result.ToString());
            Result.Remove();
        }

推荐答案

删除节点后,使用save方法将文档保存回磁盘。以下是示例代码(我根据我的示例xml文件修改了代码)。



Once you remove the node, save the document back to disk using the save method. Following is the sample code (I have modified your code as per my sample xml file).

XDocument doc = XDocument.Load("Books.xml");

var Result = (from c in doc.Descendants("book")
              where c.Attribute("id").Value == "111"
              select c).First();
Console.WriteLine(Result.ToString());
Result.Remove();
doc.Save("books1.xml");





这对我来说非常合适。

如果这个解决方案适合你,请投票:)。



This worked perfectly for me.
If this solution worked for you, please vote :).


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

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