如何删除XML中匹配的节点?
借用一下某人的XML文件做个示范:
XML文件如下:
<a>
<add key= "bb-ww " value= "100 " />
<add key= "ss " value= "100 " />
<add key= "bb-ii " value= "100 " />
<add key= "rr " value= "100 " />
<add key= "uu " value= "100 " />
<add key= "bb-pp " value= "100 " />
<add key= "kk " value= "100 " />
</a>
现在我需要找到并删除key等于rr值的该节点.要使用循环.
XmlNodeList xnl=xmlDoc.SelectSingleNode( "a ").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute( "key ")== "rr ")
{
xe.RemoveAll();
break;
}
}
为何我这样只是删除了key和value属性而已.而 <add /> 还在.
------解决方案--------------------if (xe.GetAttribute( "key ") == "rr ")
{
xe.ParentNode.RemoveChild(xe);
break;
}
------解决方案--------------------不要循环:
XmlNode xn=xmlDoc.SelectSingleNode( "//add[@key= 'rr '] ");
xn.ParentNode.Remove(xn);