views:

302

answers:

3

I have the following XML LINQ query from my XDocument.

var totals = (from x in MyDocument.Descendants("TOTALS") select x).FirstOrDefault();

Once I have found my totals node I need to add some elements to that node and push that change to the XDocument.

+1  A: 

Hi there.

you can use AddAfterSelf() to add new nodes against totals. Those changes will automatically get attached to the main XDocument, since totals is referencing an XElement inside the document.

Cheers. Jas.

Jason Evans
+1  A: 

So just make the change to the returned node... unless you clone it, it will still be part of the document.

Btw, your query expression isn't adding anything - simpler code would be:

var totals = MyDocument.Descendants("TOTALS").FirstOrDefault();
Jon Skeet
A: 
totals.Add(new XElement("NewNode", "New node value"));
Darin Dimitrov