If broken it is, fix it you should

Using the powers of the debugger to solve the problems of the world - and a bag of chips    by Tess Ferrandez, ASP.NET Escalation Engineer (Microsoft)

System.Configuration.ConfigurationErrosException “Unrecognized element ‘setting’” and other unrecognized elements

System.Configuration.ConfigurationErrosException “Unrecognized element ‘setting’” and other unrecognized elements

  • Comments 9

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

https://connect.microsoft.com/VisualStudio/feedback/details/508146/valid-rolemanager-app-config-setting-fails-to-parse

http://connect.microsoft.com/VisualStudio/feedback/details/352591/configurationerrorsexception-thrown-on-service-start-when-no-whitespace-between-value-and-setting

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

  • Hi,

    Its very nice article. Thanks

  • Thanks for verifying this behavior. Rather odd. But an easy workaround.

  • This workaround may work when you can format the data yourself. Unfortunately the RSAProtectedConfigurationProvider seems to remove all white space what leads to an error after protecting the config sections. The only workaround is to use the DpapiProtectedConfigurationProvider.

  • This workaround may work when you can format the data yourself. Unfortunately the RSAProtectedConfigurationProvider seems to remove all white space what leads to an error after protecting the config sections. The only workaround is to use the DpapiProtectedConfigurationProvider.

  • It's a bit of a shame, such an error. I get it as well. This is basic xml reading. And my config-file is encrypted, so no easy work-around. Grrrr.

  • Same issue as uwittig.  We are using the RSAProtectedConfigurationProvider to encrypt certain configuration sections, so there is no way that I know of to control the formatting of the section.  Any idea of a possible workaround for this scenario that would not require using a different encryption?

  • Hotfix available...

    support.microsoft.com/.../2548766

  • I don't seem to find this configuration file. Please help me

  • This appears to be fixed in .NET 4.5.1.

Page 1 of 1 (9 items)
Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post