自定义 .config 文件中无法识别的属性 'xmlns'

Unrecognized attribute 'xmlns' in custom .config file

我创建了一个自定义 System.Configuration.ConfigurationSection,我将其保存在一个单独的配置文件中,并通过 'configSource="MyCustomConfigFile.config"'

将其包含到我的 web.config 中

我还为自定义配置文件创建了一个 .xsd 架构,以添加一些好东西,例如架构验证/intellisense - 效果很好。

尝试启动应用程序时(托管在 IIS8、.NET 4.5.1 中) 我收到以下错误:

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

老实说,我很惊讶 - 谁能告诉我如何在不删除 xmlns 的情况下解决此问题,以便我可以保留架构 validation/intellisense?

我自己没有遇到过这个特殊问题,但您可以尝试删除项目中的 "obj" 文件夹并按照以下 post.
中的建议重建 Web.config transformation: Unrecognized attribute 'xmlns:xdt'. Note that attribute names are case-sensitive

使用找到的信息 here 很明显解析器无法反序列化配置部分,因为配置部分不知道 'xmlns' 属性 - 这实际上使完美的感觉。

为了解决这个问题,您可以将以下内容添加到 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;
                }
            }
    }

这完全解决了这个问题。