次の方法で共有


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

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

https://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

Comments

  • Anonymous
    February 17, 2010
    Hi, Its very nice article. Thanks

  • Anonymous
    July 20, 2010
    Thanks for verifying this behavior. Rather odd. But an easy workaround.

  • Anonymous
    November 07, 2010
    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.

  • Anonymous
    November 07, 2010
    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.

  • Anonymous
    January 30, 2011
    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.

  • Anonymous
    March 04, 2011
    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?

  • Anonymous
    July 08, 2011
    Hotfix available... support.microsoft.com/.../2548766

  • Anonymous
    November 18, 2013
    I don't seem to find this configuration file. Please help me

  • Anonymous
    December 05, 2013
    This appears to be fixed in .NET 4.5.1.