Delete Element in XML

I try to delete an element in Xml but currently im just removing the "sub-element" XML:

<dependency>
<dependentAssembly dependencyType="preRequisite">
  </dependentAssembly>
</dependency>

<dependency>
<dependentAssembly dependencyType="install">
</dependentAssembly>

Current Code:

string filePath = "C:\\Example\\Example.exe.manifest"
var xml = XElement.Load(filePath);
xml.Descendants().Where(x => x.Name.LocalName == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").Remove();
xml.Save(filePath);

Xml after Code:

<dependency>
 <dependentAssembly dependencyType="preRequisite">
 </dependentAssembly>
</dependency>
<dependency>
</dependency>

As you see im currently just deleting <dependentAssembly> but i try to delete <dependency>

How shall i do That?

i never did that much in XML so i try to learn from you guys :)

Jon Skeet
people
quotationmark

Two options. You could select the parent node to remove:

xml.Descendants()
   .Where(x => x.Name.LocalName == "dependentAssembly" && 
               (string)x.Attribute("dependencyType") == "install")
   .Select(x => x.Parent)
   .Remove();

Or you could use Any to find elements with any such child:

xml.Descendants()
   .Where(p => p.Elements()
                .Any(x => x.Name.LocalName == "dependentAssembly" && 
                          (string)x.Attribute("dependencyType") == "install")))
   .Remove();

I would also strongly recommend not matching by LocalName, but instead working out the full namespace-qualified element name you want, e.g.

XNamespace ns = "http://somenamespace";
xml.Descendants(ns + "dependency")
   .Where(p => p.Elements(ns + "dependentAssembly")
                .Any(x => (string)x.Attribute("dependencyType") == "install")))
   .Remove();

people

See more on this question at Stackoverflow