|
|
1 ساعت پیش | |
|---|---|---|
| .. | ||
| Apq.Cfg.Xml.csproj | 1 ساعت پیش | |
| CfgBuilderExtensions.cs | 6 روز پیش | |
| README.md | 1 روز پیش | |
| XmlFileCfgSource.cs | 1 روز پیش | |
XML 文件配置源扩展包。
仓库地址:https://gitee.com/apq/Apq.Cfg
using Apq.Cfg;
using Apq.Cfg.Xml;
var cfg = new CfgBuilder()
.AddXml("config.xml", level: 0, writeable: true)
.Build();
// 使用配置节访问
var dbSection = cfg.GetSection("Database");
var connStr = dbSection.Get("ConnectionString");
// 枚举子键
foreach (var key in dbSection.GetChildKeys())
{
Console.WriteLine($"{key}: {dbSection.Get(key)}");
}
public static CfgBuilder AddXml(
this CfgBuilder builder,
string path,
int level,
bool writeable = false,
bool optional = true,
bool reloadOnChange = true,
bool isPrimaryWriter = false)
| 参数 | 说明 |
|---|---|
path |
XML 文件路径 |
level |
配置层级,数值越大优先级越高 |
writeable |
是否可写 |
optional |
文件不存在时是否忽略 |
reloadOnChange |
文件变更时是否自动重载 |
isPrimaryWriter |
是否为默认写入目标 |
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<AppName>MyApp</AppName>
<Database>
<ConnectionString>Server=localhost;Database=mydb</ConnectionString>
<Timeout>30</Timeout>
</Database>
<Logging>
<Level>Information</Level>
</Logging>
</configuration>
配置键映射:
<AppName> -> "AppName"<Database><ConnectionString> -> "Database:ConnectionString"<Logging><Level> -> "Logging:Level"MIT License