xml - ASP.NET Core 的 XML 配置提供程序中的数组
问题描述
我正在编写的 ASP.NET Core (2.1) 应用程序使用两个配置源:JSON 和 XML(使用框架的标准AddJsonFile
和AddXmlFile
.XML 设置具有更高的优先级(例如,应该覆盖 JSON 文件中的匹配设置)。
到目前为止一切正常,直到我不得不在 XML 文件中创建一个数组。甚至可能吗?
解决方案
ASP.NET Core 中的参考配置:将数组绑定到类
Bind
支持使用配置键中的数组索引将数组绑定到对象。任何公开数字键段 (:0
,:1
, ...:{n}
) 的数组格式都能够将数组绑定到 POCO 类数组。
我将提供两个示例来说明如何在 XML 配置文件中使用数组
使用以下config.xml
<configuration>
<tvshow>
<metadata>
<series>Dr. Who</series>
<title>The Sun Makers</title>
<airdate>11/26/1977</airdate>
<episodes>4</episodes>
</metadata>
<actors name="0">Tom Baker</actors>
<actors name="1">Louise Jameson</actors>
<actors name="2">John Leeson</actors>
<legal>(c)1977 BBC https://www.bbc.co.uk/programmes/b006q2x0</legal>
</tvshow>
</configuration>
匹配的 POCO 看起来像
public class TvShow {
public Metadata Metadata { get; set; }
public string[] Actors { get; set; } //<-- TAKE NOTE
public string Legal { get; set; }
}
public class Metadata {
public string Series { get; set; }
public string Title { get; set; }
public DateTime AirDate { get; set; }
public int Episodes { get; set; }
}
鉴于<actors>
元素的名称,生成的路径将是
tvshow:actors:0
tvshow:actors:1
tvshow:actors:2
遵循文件中所述的约定
所以在下面的单元测试中它会按预期通过。
[TestClass]
public class Configuration_Should {
[TestMethod]
public void Bind_Xml_Arrays() {
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddXmlFile("config.xml", optional: true, reloadOnChange: true)
.Build();
var tvShow = config.GetSection("tvshow").Get<TvShow>();
tvShow.Should().NotBeNull();
tvShow.Actors.Should().HaveCount(3);
}
}
在另一个示例中,假设config.xml
被修改为以下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<tvshow>
<metadata>
<series>Dr. Who</series>
<title>The Sun Makers</title>
<airdate>11/26/1977</airdate>
<episodes>4</episodes>
</metadata>
<actors>
<names name="0">Tom Baker</names>
<names name="1">Louise Jameson</names>
<names name="2">John Leeson</names>
</actors>
<legal>(c)1977 BBC https://www.bbc.co.uk/programmes/b006q2x0</legal>
</tvshow>
</configuration>
将受影响的 POCO 重构为
public class TvShow {
public Metadata Metadata { get; set; }
public Actors Actors { get; set; }
public string Legal { get; set; }
}
//...Metadata omitted for brevity
public class Actors {
public string[] Names { get; set; }
}
同样,按照惯例,数组元素的路径需要看起来像
tvshow:actors:names:0
tvshow:actors:names:1
tvshow:actors:names:2
在测试时它的行为也符合预期
[TestClass]
public class Configuration_Should {
[TestMethod]
public void Bind_Xml_Arrays() {
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddXmlFile("config.xml", optional: true, reloadOnChange: true)
.Build();
var tvShow = config.GetSection("tvshow").Get<TvShow>();
tvShow.Should().NotBeNull();
tvShow.Actors.Names.Should().HaveCount(3);
}
}
这与文档一起应该为您正确构建 XML 配置以绑定到数组提供了足够的基础。
推荐阅读
- javascript - TypeError:无法读取未定义的属性“FieldValue”
- angular - Angular 9 的 ngx-translate 在 IE 中不起作用
- google-apps-script - 社区连接器教程导致“未知数据源”
- class - 为什么不能在飞镖类中的方法体之外打印任何语句
- python-3.x - 如何使用 selenium python 从 Outlook 下载嵌入的 content-id 图像
- java - Spring 跟踪日志 null ModelAndView 返回
- sql - 想要通过在 Postgres 函数中一次性传递多个参数来获得输出
- winforms - 将事件通知从 Winforms 服务器应用程序发送到 Blazor 客户端应用程序的最佳方式是什么?
- docker - 操作:推送:未经授权访问存储库 docker Harbor 注册表
- excel - 在最小最大条件范围内分布数字