.config配置文件解析(解析<appSettings>中key、value信息)

using System;
using System.Text.RegularExpressions;

public class Example
{
        /// <summary>
        /// 解析<appSettings>中key、value信息
        /// using System.Text.RegularExpressions;
        /// </summary>
        /// <returns></returns>
        public static void Main()
        {
            string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<configuration>\r\n\t<configSections>\r\n\t</configSections>\r\n\t<startup>\r\n\t\t<supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.7.2\" />\r\n\t</startup>\r\n\t<appSettings>\r\n\t\t<add key=\"ClientSettings\" value=\"\" />\r\n\t\t<add key=\"AreaLimit\" value=\"20\" />\r\n\t\t<add key=\"LastyResult\" value=\"60\" />\r\n\t\t<add key=\"serverIp\" value=\"127.0.0.1\" />\r\n\t\t<add key=\"FileEnv\" value=\"Local\"/>\r\n\t</appSettings>\r\n\t<runtime>\r\n\t</runtime>\r\n</configuration>";

            // 创建 Regex 对象并进行匹配
            Match match = Regex.Match(input, @"(?<=<appSettings>)[\s\S]*?(?=<\/appSettings>)");
            if(match.Success)
            {
                string appSettings = match.Value;

                MatchCollection matches = Regex.Matches(appSettings, @"<add.*?key=""(?<key>\w+)"".*?value=""(?<value>.+?)""");
                foreach(Match matchI in matches)
                {
                    string key = matchI.Groups["key"].Value;
                    string value = matchI.Groups["value"].Value;
					
					Console.WriteLine("Key: " + key);
					Console.WriteLine("Value: " + value);
					Console.WriteLine();
					
                }
            }

        }
}

测试地址:菜鸟教程在线编辑器


 


        /* .config
        <?xml version = "1.0" encoding="utf-8"?>
        <configuration>
	        <configSections>
	        </configSections>
	        <startup>
		        <supportedRuntime version = "v4.0" sku=".NETFramework,Version=v4.7.2" />
	        </startup>
	        <appSettings>
		        <add key = "ClientSettings" value="" />
		        <add key = "AreaLimit" value="20" />
		        <add key = "LastyResult" value="60" />
		        <add key = "serverIp" value="127.0.0.1" />
		        <add key = "FileEnv" value="Local"/>
	        </appSettings>
	        <runtime>
	        </runtime>
        </configuration>
        */

        /// <summary>
        /// 解析<appSettings>中key、value信息
        /// using System.Text.RegularExpressions;
        /// </summary>
        /// <returns></returns>
        static Dictionary<string, string> GetSettings()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<configuration>\r\n\t<configSections>\r\n\t</configSections>\r\n\t<startup>\r\n\t\t<supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.7.2\" />\r\n\t</startup>\r\n\t<appSettings>\r\n\t\t<add key=\"ClientSettings\" value=\"\" />\r\n\t\t<add key=\"AreaLimit\" value=\"20\" />\r\n\t\t<add key=\"LastyResult\" value=\"60\" />\r\n\t\t<add key=\"serverIp\" value=\"127.0.0.1\" />\r\n\t\t<add key=\"FileEnv\" value=\"Local\"/>\r\n\t</appSettings>\r\n\t<runtime>\r\n\t</runtime>\r\n</configuration>";


            // 创建 Regex 对象并进行匹配
            Match match = Regex.Match(input, @"(?<=<appSettings>)[\s\S]*?(?=<\/appSettings>)");
            if(match.Success)
            {
                string appSettings = match.Value;

                MatchCollection matches = Regex.Matches(appSettings, @"<add.*?key=""(?<key>\w+)"".*?value=""(?<value>.+?)""");
                foreach(Match matchI in matches)
                {
                    string key = matchI.Groups["key"].Value;
                    string value = matchI.Groups["value"].Value;

                    if (!dic.ContainsKey(key))
                    {
                        dic.Add(key, value);
                    }
                    else dic[key] = value;
                }
            }

            return dic;
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值