remove namespace from xml config file

从xml配置文件中移除命令空间

https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c

string xml = node.OuterXml;
//Regex below finds strings that start with xmlns, may or may not have :and some text, then continue with =
//and ", have a streach of text that does not contain quotes and end with ". similar, will happen to an attribute
// that starts with xsi.
string strXMLPattern = @"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)""";
xml = Regex.Replace(xml, strXMLPattern, "");

 

 

复制代码
 /// <summary>
        /// config section to remove in patch
        /// </summary>
        private List<string> _removeList;


        /// <summary>
        /// appSettings will keep in patch(if exist in patch)
        /// </summary>
        private List<string> _usefulAppSettings;

        /// <summary>
        /// xpath for appSettings section to remove in patch
        /// </summary>
        private string _appSettingsToRemove;
        
        private void InitIgnoreList()
        {
            InitAppSettingsToRemove();
            _removeList = new List<string>
            {
                "/configuration/connectionStrings",
                "/configuration/system.web/customErrors",
                _appSettingsToRemove
            };
        }

        private void InitAppSettingsToRemove()
        {
            //xPath = "/configuration/appSettings/add[(@key!='CMSProgrammingLanguage' and @key!='WS.webservice') ]";
            _usefulAppSettings = new List<string>
            {
                "CMSProgrammingLanguage",
                "WS.webservice",
                "ChartImageHandler",
                "PageInspector:ServerCodeMappingSupport",
                "ValidationSettings:UnobtrusiveValidationMode",
                "LISALastUpdatedVersionTime",
                "LISAUpdatedVersion"
            };
            var temp1 = "/configuration/appSettings/add[({0})]";
            var temp2 = "@key!='{0}'"; //key not equal 
            var temp3 = string.Join(" and ", _usefulAppSettings.Select(x => string.Format(temp2, x)));
            _appSettingsToRemove = string.Format(temp1, temp3);
        }
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(337)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
阅读排行:
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程
点击右上角即可分享
微信分享提示