//通过传入节点name及节点value,来删除相应节点
public static string OperateXml(string keyInfo, string valueInfo)
{
if (File.Exists(filePath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode xnRoot = xmlDoc.SelectSingleNode("根节点");
if (xnRoot == null)
{
xnRoot = xmlDoc.CreateNode(XmlNodeType.Element, "根节点", "");
xmlDoc.AppendChild(xnRoot);
}
XmlNodeList xnl = xmlDoc.SelectSingleNode("根节点").ChildNodes;
for (int i = 0; i < xnl.Count; i++)
{
XmlElement xe = (XmlElement)xnl.Item(i);
if (xe.Name.Equals(keyInfo))
{
xnRoot.RemoveChild(xe);
if (i < xnl.Count) i = i - 1;
}
}
xmlDoc.Save(filePath);
return null;
}
C# 通过传入节点name及节点value,来删除XML相应节点
最新推荐文章于 2020-12-07 14:33:42 发布
本文介绍了一种在C#中操作XML文件的方法,通过传入节点名称和值,可以实现从XML文件中删除指定节点的功能。该方法首先检查文件是否存在,然后加载文件并定位到根节点,遍历子节点并移除匹配的节点。
2288

被折叠的 条评论
为什么被折叠?



