阅读495 返回首页    go 阿里云 go 技术社区[云栖]


C# 通过传入节点name及节点value,来删除XML相应节点

//通过传入节点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;
}

最后更新:2017-04-03 12:54:18

  上一篇:go 关于优惠券中经济学与心理学
  下一篇:go Notepad++ 实用插件安装