首页 > 解决方案 > ASP.NET Core 的 XML 配置提供程序中的数组

问题描述

我正在编写的 ASP.NET Core (2.1) 应用程序使用两个配置源:JSON 和 XML(使用框架的标准AddJsonFileAddXmlFile.XML 设置具有更高的优先级(例如,应该覆盖 JSON 文件中的匹配设置)。

到目前为止一切正常,直到我不得不在 XML 文件中创建一个数组。甚至可能吗?

标签: xmlasp.net-core.net-core

解决方案


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 配置以绑定到数组提供了足够的基础。


推荐阅读