C# xml某節點下內容全部清空
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>