as i said in the Title i want to delete the node when i get a specific value out of an attribute. (in this case "install" out of the attribute "dependencyType")
Xml File:
<dependency>
<dependentAssembly dependencyType="preRequisite">
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install">
</dependentAssembly>
</dependency>
How it shall look like(after i ran through):
<dependency>
<dependentAssembly dependencyType="preRequisite">
</dependentAssembly>
</dependency>
i want to remove the node (in this case "dependency" when i get in the attribute "dependencytype" the value "install").
i tried two things 1st Code:
private void DeleteXmlPopulates()
{
string filepath = "C:\Example\Example.exe.manifest";
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
using (XmlReader reader = XmlReader.Create(filePath))
{
while (reader.Read())
{
if (reader.Name == "dependency")
{
if (reader.GetAttribute("dependencyType") == "install")
{
//here i shall get the current Node-name and .Remove(); it
}
}
}
}
2nd Code:
private void DeleteXmlPopulates()
{
string filepath = "C:\Example\Example.exe.manifest";
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
var xml = XElement.Load(File.OpenRead(filePath));
var elementsToDelete = xml.Descendants().Where(x => x.Name == "dependentAssembly" && x.Attribute("dependencyType") != null && x.Attribute("dependencyType").Value == "install");
foreach (var xElement in elementsToDelete)
{
xElement.Remove();
}
xml.Save(filePath);
}
The problem is in the first one is given in the code
The problem is in the second one is that if i get "elementsToDelete" for the foreach-loop and it comes back there arent any "elementsToDelete" so it skips the for each, after all of that, the programm tries to save (xml.save(filepath);) but it gets an exception that its already in use.
What shall i do? Why is it like that?
constructive criticism is Welcome :) correct everytihng you want in my question :) I hope its clear :)
IOException? You need to close theFileStreamreturned byFile.OpenRead