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