1.将xml转换为DataTable
string path = "";//xml的位置
StringReader sr = null;
XmlTextReader xmlReader = null;
if (File.Exists(path+"文件名"))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path+"文件名");
DataSet ds = new DataSet();
sr = new StringReader(xmlDoc.InnerXml);
xmlReader = new XmlTextReader(sr);
ds.ReadXml(xmlReader);
}
xmlReader.Close();
sr.close();
sr.Dispose();
2.删除xml里的节点
string path = "";//xml的位置
XmlDocument doc = new XmlDocment();
doc.load(path+"文件名");
XmlNode root = doc.SelectSingleNode("根节点的名字");//获取跟节点
XmlNodeList nodes = root.ChildNodes;//获取根节点
foreach(XmlNode node in nodes)
{
XmlNode idNode = node.SelectSingleNode("id"); //根据id删除
if(idNode.InnerText.Equals(id))
{
root.RemoveChild(node);
break;
}
}
3.保存节点
string path = "";//xml的位置
XmlDocument doc = new XmlDocment();
doc.load(path+"文件名");
XmlElement newNode = doc.CreateElement("Module");
XmlElement idNode = doc.CreateElement("id");
idNode.InnerText = "3";
newNode.AppendChild(idNode);
XmlNode root = doc.SelectSingleNode("根节点的名字");//获取跟节点
root.AppendChild(newNode);
doc.save(path+"文件名");
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)