5

I've created a custom System.Configuration.ConfigurationSection which I'm keeping in a separate config file and including it into my web.config via 'configSource="MyCustomConfigFile.config"'

I've also created a .xsd schema for the custom config file to add some goodies like schema validation / intellisense - which works nicely.

When attempting to start the application (Which is hosted in IIS8, .NET 4.5.1) I'm getting the following error :

Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized attribute 'xmlns'. Note that attribute names are case-sensitive.

Source Error:

Line 1: <?xml version="1.0" encoding="utf-8" ?>

Line 2: <identityServer xmlns="http://myCustomNamespace.xsd">

To be honest, I'm surprised - can anyone tell me how to fix this without removing the xmlns so that I can retain the schema validation/intellisense?

Community
  • 1
  • 1
Maciek
  • 19,435
  • 18
  • 63
  • 87

2 Answers2

12

Using the information found here It became clear that the parser is failing to deserialize the config section due to the fact that the config section is not aware of the 'xmlns' attribute - which actually makes PERFECT sense.

In order to fix this you can add the following to your custom configuration section in C# :

    public class MyCustomConfigurationSection
    {
private const string XmlNamespaceConfigurationPropertyName = "xmlns";
    [ConfigurationProperty(XmlNamespaceConfigurationPropertyName, IsRequired = false)]
            public string XmlNamespace
            {
                get
                {
                    return (string)this[XmlNamespaceConfigurationPropertyName];
                }
                set
                {
                    this[XmlNamespaceConfigurationPropertyName] = value;
                }
            }
    }

This fixes the issue completely.

Maciek
  • 19,435
  • 18
  • 63
  • 87
  • Also you should add properties for "xmlns:xsi" and "xsi:schemaLocation" if you want to specify location of your xsd schema. – Rail Jul 16 '15 at 07:01
  • Can you elaborate on that please? – Maciek Jul 16 '15 at 14:32
  • Is this the only way that you've found to remedy this issue? I mean this works fine, but seems like there should be an easier way! – crush May 12 '16 at 01:49
  • 1
    That's the only way that I'm aware of – Maciek May 12 '16 at 21:41
  • This well-written answer seems to be based in using the proper asp.net mechanisms to extend web.config in a robust way. Just my opinion - and I am trying to work this out on a legacy project I am reviving - which has a 3rd-party package that is utilizing configuration aspects of web.config. – qxotk Jul 22 '16 at 14:26
0

I haven't encountered this particular problem myself but you could try deleting the "obj" folder in your project and rebuilding as suggested in the following post.
Web.config transformation: Unrecognized attribute 'xmlns:xdt'. Note that attribute names are case-sensitive

Community
  • 1
  • 1
  • @Peter Evans - I see that you want to be helpful, and that is the right idea. If you are making suggestions that might help the person, and are not well-defined steps to the actual solution - you might want to post them as comments to the original question - where the person with the problem and confirm/deny that your suggestion is helpful. Posting these comments to the question help clarify the situation, and will not likely earn you down votes. Hope this is helpful to you. – qxotk Jul 22 '16 at 14:24