C# xml某节点下内容全部清空

来源:互联网 发布:买玩具枪被判无期知乎 编辑:程序博客网 时间:2024/05/16 05:55

1、test.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<TestList>
  <test a="" b=""/>

 <test a="" b=""/>
</TestList>

2、实现代码

           bool bSuccess = true;
            while (bSuccess)
            {
                string strTaskListPath = CommVar.curExecPath + "test.xml";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strTaskListPath);
                XmlNodeList xnl = xmlDoc.SelectSingleNode("TestList").ChildNodes;
                int nCount = xnl.Count;
                if (0 == nCount)
                {
                    bSuccess = false;
                }
                for (int i = 0; i < xnl.Count; i++)
                {
                    xnl[i].ParentNode.RemoveChild(xnl[i]);
                }
                xmlDoc.Save(strTaskListPath);
            }

3、实现结果

<?xml version="1.0" encoding="UTF-8"?>
<TestList>

</TestList>



0 0
原创粉丝点击