I need to delete specific employee node and also its child node based on the value of id. For example, here I need to delete employee tag with id="2".
<company>
<employee>
<id>1</id>
<name>sa</name>
</employee&开发者_运维问答gt;
<employee>
<id>2</id>
<name>ssa</name>
</employee>
</company>
Assuming you have loaded that into an XmlDocument
named doc
:
XmlElement el = (XmlElement)doc.SelectSingleNode("/company/employee[id=2]");
if(el != null) { el.ParentNode.RemoveChild(el); }
Try this one
XmlDocument xmlDoc = new XmlDocument();
XmlNode nodeToDelete = xmlDoc.SelectSingleNode("/root/XMLFileName[@ID="+nodeId+"]");
if (nodeToDelete != null)
{
nodeToDelete.ParentNode.RemoveChild(nodeToDelete);
}
xmlDoc.Save("XMLFileName.xml")
精彩评论