System.Configuration.ConfigurationErrorsException Unrecognized element ‘setting’ and other unrecognized elements

1 minute read

If you have a .NET configuration file with multiple <setting> elements, you may get the following exception:

Unhandled Exception: System.Configuration.ConfigurationErrorsException: Unrecognized element 'setting'. (F:\MyApp\bin\Debug\MyApp.exe.config line 11)  at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ClientSettingsStore.ReadSettings(String sectionName, Boolean isUserScoped)
   at System.Configuration.LocalFileSettingsProvider.GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
   at System.Configuration.SettingsBase.GetPropertiesFromProvider(SettingsProvider provider)
...

This happens if you have a configuration section with multiple <setting> elements like in this example

<setting name="Setting1" serializeAs="String"><value>Setting1</value></setting>
<setting name="Setting2" serializeAs="String"><value>Setting2</value></setting>

and you keep the whole <setting> element on one line.

To get rid of this error, split the <setting> element on multiple lines, or add any kind of whitespace after the </value> tag like this:

<setting name="Setting1" serializeAs="String">
   <value>Setting1</value>
</setting>

Similar Unrecognized element errors will occur if you use an expanded <clear></clear> tag instead of <clear/>. In that case you may get an Unrecognized element ‘add’ for example if <add> is the first element that appears after the expanded <clear></clear> tag. The resolution here is to simply change <clear></clear> to its short form <clear/>

We have received a few reports about this issue

but because there are very simple workarounds to both issues, and thus the potential problems that can arise from changing the code are bigger than the gain from fixing them for the clients reporting the issue, there hasn’t been a fix for this as of yet.

If for some reason you can not use the workarounds given above you can either vote up the connect bug to give it higher priority for future releases or contact support to help find a solution that fits your needs.

Laters, Tess