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

news/2024/7/7 23:11:52
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;
        }


http://lihuaxi.xjx100.cn/news/1998759.html

相关文章

Oracle 备份 还原 导入 导出 数据库

目录 导出数据 导入数据 导出数据 SQL> conn / as sysdba Connected. SQL> create directory lxw_dir as /home/oracle;Directory created.SQL> grant write,read on directory lxw_dir to lxw;Grant succeeded.SQL> exit Disconnected from Oracle Database 11…

【教程】极简Docker搭建“帕鲁幻兽PalWorld”服务器, 附资源

注意&#xff1a; 如果搭建在个人服务器或者内网中&#xff0c;需要做内网穿透&#xff0c;可以看这篇博客&#xff1a; 【教程】超详细安装和使用免费内网穿透软件Zerotier-One-CSDN博客文章浏览阅读523次&#xff0c;点赞8次&#xff0c;收藏8次。真的很详细https://blog.csd…

Packet Tracer - Configure and Verify a Site-to-Site IPsec VPN Using CLI

Packet Tracer - 使用命令行界面配置和验证站点到站点IPsec VPN 地址表 目标 验证整个网络的连通性。 配置R1以支持与R3之间的站点到站点IPsec VPN。 背景/场景 网络拓扑图显示了三个路由器。您的任务是配置R1和R3&#xff0c;以便在各自局域网&#xff08;LAN&#xff09…

Kubernetes 基础详解

大家好&#xff0c;我是升仔 Kubernetes 概述 Kubernetes起源于Google&#xff0c;它的核心理念是帮助你在集群中自动部署、扩展和运行应用程序容器。简单来说&#xff0c;它就像一个智能的容器管理员&#xff0c;能够确保你的应用始终运行在最适合的地方。 Kubernetes的基本…

(Rust)LeetCode 热题 100-两数之和

给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回…

WebSocket 整合 记录用法

WebSocket 介绍 WebSocket 是基于tcp的一种新的网络协议,可以让浏览器 和 服务器进行通信,然后区别于http需要三次握手,websocket只用一次握手,就可以创建持久性的连接,并进行双向数据传输 Http和WebSocket的区别 Http是短连接,WebSocket’是长连接Http通信是单向的,基于请求…

深度强化学习(王树森)笔记08

深度强化学习&#xff08;DRL&#xff09; 本文是学习笔记&#xff0c;如有侵权&#xff0c;请联系删除。本文在ChatGPT辅助下完成。 参考链接 Deep Reinforcement Learning官方链接&#xff1a;https://github.com/wangshusen/DRL 源代码链接&#xff1a;https://github.c…

我们是如何测试人工智能的(六)推荐系统拆解

推荐系统简介 推荐系统的问题 根据之前学习到的内容&#xff0c;我们已经基本了解到了要如何构建一个二分类模型。我们都知道模型大体可以分成&#xff0c;回归&#xff0c;二分类和多分类。但推荐系统是属于哪一种场景呢&#xff0c;比如我们常见的广告推荐或者内容推荐&…