首页 > 学院 > 开发设计 > 正文

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

2019-11-08 03:27:16
字体:
来源:转载
供稿:网友

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>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表