开发者

deleting specific node in xml

开发者 https://www.devze.com 2023-01-07 11:47 出处:网络
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\".

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")
0

精彩评论

暂无评论...
验证码 换一张
取 消