Click here to Skip to main content
15,513,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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();
        }
Posted

1 solution

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).

C#
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 :).
 
Share this answer
 
Comments
MedinaVS 11-Dec-12 14:36pm    
ahhhh, what a stupid mistake I made. Forgot the doc.save().
That happens when I learn C# until too late at night

Thanks for the reply :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900