原文发布时间为:2009-08-27 —— 来源于本人的百度文章 [由搬家工具导入]
<?xml version="1.0" encoding="gb2312"?>
<MyData>
<Item Province="北京" County="昌平" Zipcode="102200" Areacode="010">
<t>sdfsdf</t>
<t2>sdfsdf</t2>
</Item>
<Item Province="北京" County="大兴" Zipcode="102600" Areacode="010" />
<Item Province="北京" County="密云" Zipcode="101500" Areacode="010" />
</MyData>
一、XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("data.xml"));
XmlNode xn = xml.DocumentElement;
xn.RemoveAll();
xml.Save(Server.MapPath("data.xml"));
执行之后:
<?xml version="1.0" encoding="gb2312"?>
<MyData>
</MyData>
二。扩展:删除某个标记下面的所有子节点
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("data.xml"));
XmlNodeList xnl = xml.SelectNodes("MyData/Item");
foreach (XmlNode xn in xnl)
{
xn.RemoveAll();
}
xml.Save(Server.MapPath("data.xml"));
执行以后:
<?xml version="1.0" encoding="gb2312"?>
<MyData>
<Item>
</Item>
<Item />
<Item />
</MyData>