C#操作XML移除节点的问题
源文件:
<?xml version= "1.0 " encoding= "gb2312 "?>
<bookstore>
<book genre= "fantasy " ISBN= "2-3631-4 ">
<title> Oberon 's Legacy </title>
<author> Corets, Eva </author>
<price> 5.95 </price>
</book>
<book genre= "update李赞红 " ISBN= "2-3631-4 ">
<title> CS入门到精通 </title>
<author> 亚胜 </author>
<price> 58.3 </price>
</book>
</bookstore>
事件处理程序:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load( "bookstore1603.xml ");
XmlNodeList xnl = xmlDoc.SelectSingleNode( "bookstore ").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute( "genre ") == "fantasy ")
{
xe.RemoveAttribute( "genre ");
}
else if (xe.GetAttribute( "genre ") == "update李赞红 ")
{
xe.RemoveAll();
}
}
xmlDoc.Save( "bookstore1604.xml ");
目标文件:
<?xml version= "1.0 " encoding= "gb2312 "?>
<bookstore>
<book ISBN= "2-3631-4 ">
<title> Oberon 's Legacy </title>
<author> Corets, Eva </author>
<price> 5.95 </price>