Linq to XML - update/alter the nodes of an XML Document

I've got 2 Questions:

1. I've sarted working around with Linq to XML and i'm wondering if it is possible to change an XML document via Linq. I mean, is there someting like

XDocument xmlDoc = XDocument.Load("sample.xml");

update item in xmlDoc.Descendants("item")
where (int)item .Attribute("id") == id
...

2. I already know how to create and add a new XMLElement by simply using

xmlDoc.Element("items").Add(new XElement(......);

but how can I remove a single entry?

XML sample data:

<items>
  <item id="1" name="sample1" info="sample1 info" web="" />
  <item id="2" name="sample2" info="sample2 info" web="" />
</itmes>

Solution 1:

thank you for your answer. everything works fine.

just as completition to my questions the code below shows how to modify a single entry:

string xml = @"<data><record id='1' info='sample Info'/><record id='2' info='sample Info'/><record id='3' info='sample Info'/></data>";
StringReader sr = new StringReader(xml);
XDocument d = XDocument.Load(sr);


d.Descendants("record").Where(x => x.Attribute("id").Value == "2").Single().SetAttributeValue("info", "new sample info");

Solution 2:

Is this what you have in mind?

using System;
using System.Linq;
using System.Xml.Linq;

static void Main(string[] args)
{
    string xml = @"<data><record id='1'/><record id='2'/><record id='3'/></data>";
    StringReader sr = new StringReader(xml);
    XDocument d = XDocument.Load(sr);

    // the verbose way, if you will be removing many elements (though in
    // this case, we're only removing one)
    var list = from XElement e in d.Descendants("record")
               where e.Attribute("id").Value == "2" 
               select e;

    // convert the list to an array so that we're not modifying the
    // collection that we're iterating over
    foreach (XElement e in list.ToArray())
    {
       e.Remove();
    }

    // the concise way, which only works if you're removing a single element
    // (and will blow up if the element isn't found)
    d.Descendants("record").Where(x => x.Attribute("id").Value == "3").Single().Remove();

    XmlWriter xw = XmlWriter.Create(Console.Out);
    d.WriteTo(xw);
    xw.Flush();
    Console.ReadLine();
}

Solution 3:

The answers are in this thread...you just have to do a lot of sorting to find them, so I've done the work of compliling them for you:

  1. YES you can edit elements
  2. Deleting elements is easy: element.Remove(); (Remember to save the xDocument after that)

Now if you're reading this thread, you probably want to know HOW to edit elements. There are two different ways that data is stored in xml, e.g.:

<tagName attributeName="some value">another value</tagName>
  1. As an ATTRIBUTE on a tag
  2. As the content (read value) of the tag

To edit the value of an attribute, knox answered his own question:

d.Descendants("record").Where(x => x.Attribute("id").Value == "2").Single().SetAttributeValue("info", "new sample info");

In other words, get the XElement that you want to alter, and call element.SetAttributeValue("AttributeName", "new value for the attribute")

if you want to edit the value or contents of a tag, then Ajay answered it (if you dig through all his code):

persondata.Element("City").Value = txtCity.Text;

Or, in other words, once you have the XElement you're after, just use .Value and assign away.

Remember that after you perform any of these modifications on the elements in memory, you've got to call .Save() on the XDocument if you want to persist those changes to disk.