-1

Possible Duplicate:
How to delete xml element with C#?

Say I have an XML Document like so :

<doc>
     <car>
         <color>Red</color>
         <size>compact</size>
     </car>
     <car>
         <color>Yellow</color>
         <size>mid-size</size>
     </car>
</doc>

And I wanted to delete the entire entry of car with the attribute yellow, so the xml file just becomes

<doc>
     <car>
         <color>red</color>
         <size>compact</size>
     </car>
</doc>

How would I go about this? I've searched high and low and found solutions, but sadly not for c#.

Community
  • 1
  • 1
Kefkamaydie
  • 127
  • 1
  • 2
  • 11
  • What is your criteria for searching this specific node? – Tariqulazam Oct 12 '12 at 01:26
  • 4
    Did you really search? Your answer can be found by almost the first link in google by "C# xml delete" – horgh Oct 12 '12 at 01:29
  • All attributes of the node would be known. In my case it's an ID number and a DateTime object. So I suppose I would search for the node just using the ID number assigned to it. – Kefkamaydie Oct 12 '12 at 01:30

3 Answers3

1

You could use Linq to XML:

  var yellowCars = from car in xDoc.Elements("doc").Elements("car") 
                     where car.Element("Color").Value == "Yellow" 
                     select car;

    foreach (var car in yellowCars)
    {
        car.Remove();
    }

  xDoc.Save();
Mike Parkhill
  • 5,511
  • 1
  • 28
  • 38
0

How I always handle this situation: I load the entire file into memory, you can either do this by deserializing the xml into c# classes or into the xmlnode structure, there are plenty of tutorials on that. Afterwords I make any edits to the list or array and then reserialize and overwrite the entire file. Provide your file doesn't have many hundreds of records there should be no noticeable performance problems

Whistler
  • 174
  • 6
  • Unless those records are gargantuan, even "many hudreds" probably isn't a lot. IO is a more likely candidate for a performance bottleneck than DOM parsing, and even streaming APIs have to sequentially read the whole file and then write it out. Their advantage is when working with documents that just can not be buffered without paging out the rest of your working set. – millimoose Oct 12 '12 at 01:50
  • if your actual data is as simple as your example then the answer below is better. My data was fairly complex so was easier to manipulate in the serializable classes than it was to use the xml syntax – Whistler Oct 12 '12 at 01:52
0
using System.Xml.XPath;

...

var doc = XDocument.Parse(xml);
var list = new List<XElement>();
foreach (var element in doc.XPathSelectElements(@"/doc/car[color='Yellow']")) {
    list.Add(element);
}
foreach (var element in list) element.Remove();

UPDATED: so it wasn't altering a collection while iterating it.

Rob
  • 517
  • 3
  • 10
  • Typically calling `Remove` when within a `foreach` loop causes an exception, is that not the case with `XPathSelectElements`? – Guvante Oct 12 '12 at 01:39