anjel
anjel

Reputation: 1384

delete child node with specific value

Am trying to delete a list name containing the value "dfdfdfd" from an XPath

XmlNode names = LoadDocument(xml).DocumentElement.SelectSingleNode("//Class[@Name='" + getCurClass() + "']/Property[@Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");

after I execute this statement:

names.RemoveChild(names.FirstChild); 

but I nothing happens.

My XML :

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Class Name="ECMInstruction" Style="Top">
    <Entity Id="1" Name="DocumentInformation" />
    <Property Id="1">
    </Property>
    <Property Id="2">
      <Lists>
        <ListName>ws_Users</ListName>
         <ListName>dfdfdfd</ListName>
      </Lists>
    </Property>
  </Class>
</Root>

Thanks for the help

Upvotes: 0

Views: 79

Answers (1)

har07
har07

Reputation: 89285

You need to save the modified XmlDocument object back to file :

XmlDocument doc = LoadDocument(xml);
XmlNode names = doc.DocumentElement.SelectSingleNode("//Class[@Name='" + getCurClass() + "']/Property[@Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");
names.RemoveChild(names.FirstChild); 
//save `doc` back to file :
doc.Save("path_to_the_xml_file.xml");

or this way :

XmlNode names = LoadDocument(xml).DocumentElement.SelectSingleNode("//Class[@Name='" + getCurClass() + "']/Property[@Id='" + i + "']/Lists[contains(ListName,'ws_Users')]");
names.RemoveChild(names.FirstChild); 
//save owner `XmlDocument` back to file :
names.OwnerDocument.Save("path_to_the_xml_file.xml");

Upvotes: 2

Related Questions