Delete element with a specific attribute



As i said in the title :


I want to delete the whole element when i do not get "preRequisite" in the attribute:


(XML) First Element:



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


(XML) Second Element:



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


Current Code:



private void Deletepopulates(string filepath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filepath);
foreach(var nodeToDelete in new List<XmlNode>(doc.SelectNodes(filepath + "[@dependencyType!='preRequisite']").Cast<XmlNode>()))
{
if (nodeToDelete != null)
{
nodeToDelete.ParentNode.RemoveChild(nodeToDelete);
}
doc.Save(filepath);
}
}


Current it does only delete the Child (dependencyType) but it shall delete the element (dependency), is there a way to delete dependency ?


and at: new List<XmlNode>(doc.SelectNodes(filepath + "[@dependencyType!=preRequisite").Cast<XmlNode>())i get an Exception called: System.Xml.XPath.XPathException


Hopfully its clear otherwhise i'll edit it in your fovor :)


No comments:

Post a Comment