0

is it possible to add child objects to xml file using ExeConfiguration? I got stuck on this code and I can't manage to figure it out. I came up it's not that simple to just add an object to ConfigurationSection as ConfigurationElement. Is there any way to achieve what am I looking for?

That's my configuration section

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;
        }
    }

That's the child element I expect to be written to an xml file

    {
        [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";

        ...
    }

That's my xml file I got so far

<?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>

That's the way that I handle saving configuration on WinForms FormClosed event

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);
        }

That's the helper method that I use to open Exe configuration

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);
        }
    }

1 Answers1

0

It appears that my solution didn't work because it basically didin't have any sense. To properly save data I had to change default setters and getters of my properties in ConfigurationElement.

E.g.

From:

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

To:

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