首页 > 其他 > 详细

从配置文件中删除节点

时间:2018-05-14 16:21:29      阅读:168      评论:0      收藏:0      [点我收藏+]

 

 

  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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!