日期:2014-05-18  浏览次数:20771 次

xml 删除节点问题 急等
XML code
<?xml version="1.0" encoding="utf-8"?>
<graph xaxisname="51KH.CN" yaxisname="Export" hovercapbg="DEDEBE" hovercapborder="889E6D" rotateNames="0" yAxisMaxValue="600" numdivlines="9" divLineColor="CCCCCC" divLineAlpha="80" decimalPrecision="0" showAlternateHGridColor="1" AlternateHGridAlpha="30" AlternateHGridColor="CCCCCC">
    <categories font='Arial' fontSize='11' fontColor='000000'>
        <category name='3/1' />
        <category name='3/2' />
        <category name='3/3' />
        <category name='3/4' />
        <category name='3/5' />
        <category name='3/6' />
        <category name='3/7' />
    </categories>
    <dataset seriesname='PV' color='FDC12E'>
        <set value='30' />
        <set value='26' />
        <set value='29' />
        <set value='31' />
        <set value='64' />
        <set value='34' />
        <set value='94' />
    </dataset>
    <dataset seriesname='IP' color='56B9F9'>
        <set value='67' />
        <set value='98' />
        <set value='79' />
        <set value='73' />
        <set value='80' />
        <set value='34' />
        <set value='134' />
    </dataset>
</graph>


我想删除所有节点
只留下
XML code
<?xml version="1.0" encoding="utf-8"?>
<graph xaxisname="51KH.CN" yaxisname="Export" hovercapbg="DEDEBE" hovercapborder="889E6D" rotateNames="0" yAxisMaxValue="600" numdivlines="9" divLineColor="CCCCCC" divLineAlpha="80" decimalPrecision="0" showAlternateHGridColor="1" AlternateHGridAlpha="30" AlternateHGridColor="CCCCCC">
</graph>


------解决方案--------------------
C# code

XmlNodeList xnl = xmlDoc.SelectSingleNode("graph").ChildNodes;

            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
               if (xe.GetAttribute("xaxisname") == "51KH.CN")
                   xe.RemoveAll();//删除该节点的全部内容  
            }
            xmlDoc.Save("bookstore.xml");

------解决方案--------------------
C# code

 XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/Content/Url.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("graph ").ChildNodes;//获取节点集合
foreach (XmlNode item in nodeList)
{
//可以判断节点名称 来删除
item.RemoveAll();//移除当前节点
}
xmlDoc.Save("URL");//保存

------解决方案--------------------
C# code

      XmlDocument doc = new XmlDocument();
      doc.Load(@"C:\aaa.xml");
      XmlElement rootel = doc.DocumentElement;
      if (rootel != null)
      {
        for (int i = rootel.ChildNodes.Count - 1; i >= 0; i--)
        {
          rootel.RemoveChild(rootel.ChildNodes[i]);
        }
        doc.Save(@"C:\aaa.xml");
      }