Click here to Skip to main content
14,455,338 members
Rate this:
Please Sign up or sign in to vote.
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.

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100