在C#里删除XML节点?
<?xml version= "1.0 " encoding= "gb2312 "?>
<contact>
<person>
<name> x </name>
<number> 12344556 </number>
<email> poiuyt </email>
<ct> lkjhg </ct>
</person>
<person>
<name> wsx </name>
<number> edc </number>
<email> rfv </email>
<ct> tgb </ct>
</person>
</contact>
XML如上,想删除 name=x 这个节点,运用程序:
XmlDocument xdoc = new XmlDocument();
xdoc.Load( "E:\\BegC#XML\\myPhoBk.xml ");
XmlNodeList xList = xdoc.SelectNodes(@ "//*[text()= ' " + deleNm + " ']/parent::* ");
foreach (XmlNode xNode in xList)
{
xNode.RemoveAll();
}
删除后输出XML如下:
<?xml version= "1.0 " encoding= "gb2312 "?>
<contact>
<person>
</person>
<person>
<name> wsx </name>
<number> edc </number>
<email> rfv </email>
<ct> tgb </ct>
</person>
</contact>
想把包含 name=x 节点的父节点 <person>一起删除.请问该怎么做?
------解决方案--------------------foreach (XmlNode node in xList)
{
if (node.ParentNode != null)
{
node.ParentNode.RemoveChild(node);
}
}