0

是否可以使用 ExeConfiguration 将子对象添加到 xml 文件?我被困在这段代码上,我无法弄清楚。我想将一个对象作为 ConfigurationElement 添加到 ConfigurationSection 并不是那么简单。有什么方法可以实现我想要的吗?

这是我的配置部分

public class DataEnhancementToolConfigurationSection : ConfigurationSection
{
        [ConfigurationProperty(ConfigKeys.GoogleApiKeys)]
        public GoogleApiKeysConfigurationElementCollection GoogleApiKeys
        {
            get => (GoogleApiKeysConfigurationElementCollection)this[ConfigKeys.GoogleApiKeys];
            set => this[ConfigKeys.GoogleApiKeys] = value;
        }

        [ConfigurationProperty(ConfigKeys.NameNormalizationAndGeocodingOptions)]
        public NameNormalizationOptionsConfigurationElement NameNormalizationAndGeocodingOptions
        {
            get => (NameNormalizationOptionsConfigurationElement)this[ConfigKeys.NameNormalizationAndGeocodingOptions];
            set => this[ConfigKeys.NameNormalizationAndGeocodingOptions] = value;
        }
    }

这是我希望写入 xml 文件的子元素

    {
        [DisplayName("Place id")]
        [Category("Print")]
        [ConfigurationProperty(ConfigKeys.PrintPlaceId)]
        public bool PrintPlaceId { get; set; } = true;

        [DisplayName("Place id")]
        [Category("Column name")]
        [ConfigurationProperty(ConfigKeys.PlaceIdColumnName)]
        public string PlaceIdColumnName { get; set; } = "PlaceId";

        [DisplayName("Company name")]
        [Category("Print")]
        [DefaultValue(true)]
        [ConfigurationProperty(ConfigKeys.PrintCompanyName)]
        public bool PrintCompanyName { get; set; } = true;

        [DisplayName("Company name")]
        [Category("Column name")]
        [ConfigurationProperty(ConfigKeys.CompanyNameColumnName)]
        public string CompanyNameColumnName { get; set; } = "CompanyName";

        ...
    }

这是我到目前为止得到的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="data-enhancement-tool-config" type="DataEnhancementTool.Configuration.DataEnhancementToolConfigurationSection, DataEnhancementTool, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowExeDefinition="MachineToLocalUser" />
    </configSections>
    <data-enhancement-tool-config>
        <google-api-keys>
            <add google-key=":)" />
        </google-api-keys>
    </data-enhancement-tool-config>
</configuration>

这就是我在 WinForms FormClosed 事件上处理保存配置的方式

private void NameNormalizationAndGeocodingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            var config =
                ConfigurationHelper
                    .GetSectionFromRoamingConfiguration<DataEnhancementToolConfigurationSection>(
                        ConfigKeys.DataEnhancementSectionName);

            config.section.NameNormalizationAndGeocodingOptions.CityColumnName 
                = _nameNormalizationOptionsConfigurationElement.CityColumnName;
            // ... modify properties

            config.configFile.Save(ConfigurationSaveMode.Modified);
        }

这是我用来打开Exe配置的辅助方法

public static class ConfigurationHelper
    {
        public static (Configuration configFile, T section) GetSectionFromRoamingConfiguration<T>(string sectionKey) 
            where T : ConfigurationSection, new()
        {
            var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
            var section = (T)configuration.GetSection(sectionKey) ?? new T();
            section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
            return (configuration, section);
        }
    }
4

1 回答 1

0

看来我的解决方案不起作用,因为它基本上没有任何意义。为了正确保存数据,我必须在 ConfigurationElement 中更改我的属性的默认设置器和获取器。

例如

从:

[ConfigurationProperty(ConfigKeys.UrlColumnName, IsRequired = true, DefaultValue = "Url")]
public string UrlColumnName { get; set; }

至:

[ConfigurationProperty(ConfigKeys.UrlColumnName, IsRequired = true, DefaultValue = "Url")]
public string UrlColumnName
{
    get => (string) this[ConfigKeys.UrlColumnName]; 
    set => this[ConfigKeys.UrlColumnName] = (string) value;
}
于 2019-09-20T12:45:12.867 回答