I have an xml document which looks like this:
xd.Descendants("add")
.First(a => a.Attribute("key").Value == "version")
.Remove();
If you have tags other than myApp
under Applications
containing add
, you may prefer a safer version
xd.Descendants("myApp").First()
.Descendants("add")
.Where(x => (string)x.Attribute("key") == "version")
.Remove();
You can also use XPath (System.Xml.XPath)
string key="version";
xd.XPathSelectElement(String.Format("//myApp/add[@key='{0}']",key)).Remove();
string key = "version";
XDocument xdoc = XDocument.Load(path_to_xml);
xdoc.Descendants("add")
.Where(x => (string)x.Attribute("key") == key)
.Remove();
UPDATE You almost did the job. What you missed is filtering elements by attribute value. Here is your code with filtering and removing selected elements:
xd.Element("Applications")
.Element("myApp")
.Elements("add")
.Where(x => (string)x.Attribute("key") == key)
.Remove();