码迷,mamicode.com
首页 > 其他好文 > 详细

从配置文件中删除节点

时间:2018-05-14 16:47:56      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:line   pat   color   app   system   cms   ati   error   ror   

 

 

  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;
            }
        }

 

从配置文件中删除节点

标签:line   pat   color   app   system   cms   ati   error   ror   

原文地址:https://www.cnblogs.com/chucklu/p/9036504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!