删除XML文件:
string localPath = Application.streamingAssetsPath + "/" + "SchoolReport.xml";
if (File.Exists(localPath))
{
XmlDocument xmlDoc = new XmlDocument();
//加载文件
xmlDoc.Load(localPath);
//查找根节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("xml").ChildNodes;
XmlElement delItem = null;
//查找根节点下子节点
foreach (XmlElement item in nodeList)
{
//查找子节点下的节点
foreach (XmlElement ite in item.ChildNodes)
{
if (ite.InnerText == "根据数据查找到想要删除的节点")
{
//将这个子节点保存并跳出
delItem = item;
break;
}
}
if (delItem != null)
{
break;
}
}
//根据根节点删除整个保存下来的子节点
xmlDoc.SelectSingleNode("xml").RemoveChild(delItem);
//保存文件
xmlDoc.Save(localPath);
}
读取XML文件:
string localPath = Application.streamingAssetsPath + "/" + "SchoolReport.xml";
if (File.Exists(localPath))
{
XmlDocument xmlDoc = new XmlDocument();
//加载文件
xmlDoc.Load(localPath);
//查找根节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("xml").ChildNodes;
//查找根节点下子节点
foreach (XmlElement item in nodeList)
{
//查找子节点下的节点
foreach (XmlElement ite in item.ChildNodes)
{
Debug.Log(ite.InnerText);
}
}
//保存文件
xmlDoc.Save(localPath);
}
|