Reputation: 87
As i said in the Title: I want to Delete a whole element in a Xml-File with c# if the value of an attribute of this element is preRequisite.
XML File :
<dependency>
<dependentAssembly dependencyType="preRequisite"></dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install"></dependentAssembly>
</dependency>
current Code:
using (XmlReader reader = XmlReader.Create(filePath))//filepath is just a normal Path
{
while (reader.Read())
{
if (reader.Name == "dependentAssembly" && reader.GetAttribute("dependencyType") != "preRequisite")
{
//here shall it delete the Element "dependency"
}
}
}
At the place where i set the command it shall delete the Element "dependency"
i tried many things but i dont get how i shall delete this Element
Upvotes: 0
Views: 68
Reputation: 149608
You can achieve this using LINQ to XML:
var xml = XElement.Load(File.OpenRead(@"XmlLocation"));
var elementsToDelete =
xml.Descendants("dependentAssembly")
.Where(x => x.Attribute("dependencyType") != null &&
x.Attribute("dependencyType").Value != "preRequisite");
foreach (var xElement in elementsToDelete)
{
xElement.Remove();
}
Upvotes: 2