【问题标题】:Can I parse app.config custom sections to List data structure?我可以将 app.config 自定义部分解析为列表数据结构吗?
【发布时间】:2018-10-01 01:09:09
【问题描述】:

我有以下 app.config 自定义部分:

<myCustomSection>
    <person1 name = "John" age = "30">
    <person2 name = "Mike" age = "46">
</muCustomSection>

下面的代码在 System.Configuration.ConfigurationManager 的帮助下解析它:

public class MyCustomCongifuration : ConfigurationSection {
     [ConfigurationProperty("person1", IsRequired = true)]
     public PersonConfiguration Person1
     {
         get => (PersonConfiguration)this["person1"];
         set => this["person1"] = value;
     }

     [ConfigurationProperty("person2", IsRequired = true)]
     public PersonConfiguration Person2
     {
         get => (PersonConfiguration)this["person2"];
         set => this["person2"] = value;
     }
}

我想重构代码以使用以下 app.config:

<myCustomSection>
    <person name = "John" age = "30">
    <person name = "Mike" age = "46">
</muCustomSection>

我希望在 MyCustomConfiguration 对象中获得 PersonConfiguration 列表:

var configuration = ConfigurationManager.GetSection("myCustomSection") as MyCustomConfiguration;
var persons = configuration.Persons; // returns List<PersonConfiguration>

有可能吗?我该怎么做?

【问题讨论】:

    标签: c# .net configuration app-config


    【解决方案1】:

    我不确定如何使用 ConfigurationManager 做到这一点,但可以使用不同的方法来使用 XML Linq:

    using System.Xml.Linq;
    
    XDocument doc = XDocument.Load("C:\path\to\file.config");
    var persons = doc.Element("configuration").Element("myCustomSection").Descendants("person");
    
    foreach (var person in persons)
    {
        var Name = Attribute("name").Value;
        var Age = Attribute("age").Value;
    }
    

    您可以使用以下方法删除项目:

    doc.Element("configuration").Elements("myCustomSection").First(s =>(string)s.Attribute("name") == "Name you want to find").Remove();
    

    【讨论】:

    • 什么样的人?
    • persons 假定类型为 IEnumerable,每个人都是 XElement 类型。
    【解决方案2】:

    .NET Framework 确实支持在其配置中包含项目列表,但与您列出的不完全一样。如果您查看文件的 appSettings 部分,您会看到一个集合示例:

    <appSettings>
      <add key="foo" value="Fizz" />
      <add key="bar" value="Buzz" />
    </appSettings>
    

    这是基于专用集合 ConfigurationElementCollection 构建的,它允许您将 add remove 和 clear 函数建模为 XML 以构建您的项目列表。

    首先,将PersonConfiguration 的name 属性标记为键。我假设此人的姓名适合作为关键标识符;如果您想出不同的标识符,请使用它。

    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }
    

    创建从ConfigurationElementCollection 派生的自定义集合,以便返回一个人的键值。

    [ConfigurationCollection(typeof(PersonConfiguration))]
    public class PersonConfigurationElementCollection : ConfigurationElementCollection
    {
        protected override PersonConfiguration CreateNewElement()
        {
            return new PersonConfiguration();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PersonConfiguration)element).Name;
        }
    }
    

    然后将此集合添加到您的自定义配置部分的类中:

    public class MyCustomCongifuration : ConfigurationSection
    {
         [ConfigurationProperty("people", IsDefaultCollection = true)]
         public PersonConfigurationElementCollection People
         {
             get { return (PersonConfigurationElementCollection)this["people"]; }
             set { this["people"] = value; }
         }
    }
    

    您现在可以这样创建人员集合:

    <myCustomSection>
      <people>
        <add name="John" age="30" />
        <add name="Mike" age="46" />
      </people>
    </muCustomSection>
    

    如果你想把那个配置集合变成一个列表,你可以用一点Linq:

    var configuration = (MyCustomConfiguration)ConfigurationManager
        .GetSection("myCustomSection");
    var people = configuration.People.Cast<PersonConfiguration>().ToList();
    

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多