从配置文件中删除节点

  private List<string> _ignoreList;

        private void InitIgnoreList()
        {
            _ignoreList = new List<string>
            {
                "/configuration/appSettings/add[(@key='CMSTrialKey')]",
                "/configuration/appSettings/add[(@key='CMSApplicationGuid')]",
                "/configuration/appSettings/add[(@key='CMSApplicationName')]",
                "/configuration/connectionStrings",
                "/configuration/system.web/customErrors",
                "/configuration/appSettings/add[(@key='LISALastUpdatedVersionTime')]",
                "/configuration/appSettings/add[(@key='LISAUpdatedVersion')]"
            };
        }
 public void RemoveIgnore(XmlDocument doc)
        {
            foreach (var xpath in _ignoreList)
            {
                var nodeList = doc.SelectNodes(xpath); // apply your xpath here
                if (nodeList == null)
                {
                    continue;
                }
                foreach (XmlNode node in nodeList)
                {
                    Console.WriteLine(node.OuterXml);
                    RemoveChildNode(node);
                }
            }
        }

        private void RemoveChildNode(XmlNode childNode)
        {
            var parentNode = childNode.ParentNode;
            if (parentNode == null) return;
            parentNode.RemoveChild(childNode);
            while (parentNode != null && !parentNode.HasChildNodes)
            {
                var ancestorNode = parentNode.ParentNode;
                ancestorNode?.RemoveChild(parentNode);
                parentNode = ancestorNode;
            }
        }
原文地址:https://www.cnblogs.com/chucklu/p/9036504.html