0

我知道这个主题已经在许多不同的 Stackoverflow 文章中有所涉及,我已经阅读了大约 30 篇文章,以确保我所做的与这些文章相匹配。它是(甚至来自 .Net 2.0、3.0 和 4.0 版本的答案)

我正在尝试创建一个非常简单(至少在我看来)的配置文件,其中包含部分的自定义属性,然后是部分中的可选项目。所以,现在到代码:

<?xml version="1.0" encoding="utf-8" ?>
<CustomSiteConfiguration>
    <Sites>
        <Site siteRoot="/Site US" name="SiteUS_en">
        </Site>
        <Site siteRoot="/Site Canada" name="SiteCanada_en">
        </Site>
        <Site siteRoot="/Partner" name="Partner_en">
            <siteSettings>
                <setting name="" value="" />
            </siteSettings>
            <JavaScriptBundles>
                <file name="" />
            </JavaScriptBundles>
            <CSSBundles>
                <file name="" />
            </CSSBundles>
        </Site>
    </Sites>
</CustomSiteConfiguration>

因此,您正在查看的是Sites类型的全局 Section,其中包含Site类型的多个部分 (CollectionElementCollections) 。站点由项目的自定义属性以及部分本身的可选项目定义。所以,siteSettings 是可选的,JavaScriptBundles 是可选的,CSSBundles 也是可选的。

C#代码如下:

对于网站

public class CustomGlobalSiteConfiguration : ConfigurationSection
{
    public CustomGlobalSiteConfiguration() { }

    [ConfigurationProperty("Sites")]
    [ConfigurationCollection(typeof(SitesCollection), AddItemName="Site")]
    public SitesCollection Sites
    {
        get
        {
            return (SitesCollection)base["Sites"];
        }
    }
}

对于网站集

[ConfigurationCollection(typeof(SitesCollection), AddItemName="Site")]
public class SitesCollection : ConfigurationElementCollection
{
    // Constructor
    public SitesCollection() { }

    /*
    public CustomSiteConfiguration this[int index]
    {
        get { return (CustomSiteConfiguration)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
                BaseAdd(index, value);
        }
    }   // end of public siteSetting this [int index]
     * */

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((CustomSiteConfiguration)element).name;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new SitesCollection();
    }
}

对于站点定义

/**
 * Overarching structure of the Site Item
 **/
public class CustomSiteConfiguration : ConfigurationElement
{
    [ConfigurationProperty("siteRoot")]
    public String siteRoot
    {
        get
        {
            return (String)this["siteRoot"];
        }
        set
        {
            this["siteRoot"] = value;
        }
    }

    [ConfigurationProperty("name")]
    public String name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }

    [ConfigurationProperty("siteSettings", IsRequired=false)]
    public CustomSiteSiteSettings siteSettings 
    { 
        get 
        { 
            return this["siteSettings"] as CustomSiteSiteSettings;
        }
    }

    [ConfigurationProperty("JavaScriptBundles", IsRequired = false)]
    public JavaScriptBundles javaSciptBundle
    {
        get
        {
            return this["JavaScriptBundles"] as JavaScriptBundles;
        }
    }


    [ConfigurationProperty("CSSBundles", IsRequired = false)]
    public CSSBundles cssBundle
    {
        get
        {
            return this["CSSBundles"] as CSSBundles;
        }
    }


}       // end of public class CustomSiteConfiguration : ConfigurationSection

对于 SiteSettings 定义

/**
 * Subsection - Site Settings
 **/ 
public class CustomSiteSiteSettings : ConfigurationElementCollection
{
    // Constructor
    public CustomSiteSiteSettings() { }

    public siteSetting this [int index]
    {
        get { return (siteSetting)BaseGet(index);  }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
                BaseAdd(index, value);
        }
    }   // end of public siteSetting this [int index]

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((siteSetting)element).name;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new CustomSiteSiteSettings();
    }

}       // end of public class CustomSiteSiteSettings : ConfigurationSection

场地设置元素

public class siteSetting : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public String name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }   // end of public String name

    [ConfigurationProperty("value")]
    public String value
    {
        get
        {
            return (String)this["value"];
        }
        set
        {
            this["value"] = value;
        }
    }   // end of public String value
}       // end of public class siteSetting : ConfigurationElement

我省略了其他项目的空间,但其他部分看起来一样。基本上,正在发生的事情是,我得到

无法识别的属性“siteRoot”。请注意,属性名称区分大小写。

看着一切,似乎我应该没事,但是,我认为我可能做的太多而错过了一些事情。对此的任何帮助将不胜感激。

谢谢

4

1 回答 1

1

我已经弄清楚我的代码出了什么问题。我将提供以下信息。我使用以下文章来帮助跟踪其中的一些部分:如何使用 ConfigurationElementCollection 实现 ConfigurationSection

我将整个代码库归于无有,并从头开始构建它。XML 还是一样的

<?xml version="1.0" encoding="utf-8" ?>
<CustomSiteConfiguration>
<Sites>
        <Site siteRoot="/Site US" name="SiteUS_en">
        </Site>
        <Site siteRoot="/Site Canada" name="SiteCanada_en">
        </Site>
        <Site siteRoot="/Partner" name="Partner_en">
            <siteSettings>
                <setting name="" value="" />
            </siteSettings>
            <JavaScriptBundles>
                <file name="" />
            </JavaScriptBundles>
            <CSSBundles>
                <file name="" />
            </CSSBundles>
        </Site>
    </Sites>
</CustomSiteConfiguration>

所以,首先我从站点容器开始

public class CustomSiteSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("Sites")]
    [ConfigurationCollection(typeof(SiteCollection), AddItemName="Site")]
    public SiteCollection Sites
    {
        get
        {
            return (SiteCollection)base["Sites"];
        }
    }           // end of public SiteCollection Site
}               // end of public class CustomSiteSettings : ConfigurationSection { 

然后我为网站元素集合添加了 SiteCollection

public class SiteCollection : ConfigurationElementCollection
{
    // Constructor
    public SiteCollection() { }

    public SiteElement this[int index]
    {
        get { return (SiteElement)BaseGet(index);  }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }       // end of public SiteElement this[int index]

    protected override ConfigurationElement CreateNewElement()
    {
        return new SiteElement();
    }       // end of protected override ConfigurationElement CreateNewElement()

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((SiteElement)element).name;
    }
}           // end of public class SiteCollection : ConfigurationElementCollection

然后我添加了带有可选值的站点定义

public class SiteElement : ConfigurationElement
{
    // Constructor
    public SiteElement() { }

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }           // end of public String name

    [ConfigurationProperty("siteRoot", IsRequired = true)]
    public String siteRoot
    {
        get { return (String)this["siteRoot"]; }
        set { this["siteRoot"] = value; }
    }           // end of public String siteRoot

    [ConfigurationProperty("siteSettings", IsRequired=false)]
    [ConfigurationCollection(typeof(SiteSettingsElementCollection), AddItemName = "setting")]
    public SiteSettingsElementCollection siteSettings
    {
        get
        {
            return (SiteSettingsElementCollection)base["siteSettings"];
        }
    }           // end of public SiteCollection Site
}               // end of public class SiteElement : ConfigurationElement

接下来我添加了 SiteSettings 集合

public class SiteSettingsElementCollection : ConfigurationElementCollection
{
    // Constructor
    public SiteSettingsElementCollection() { }

    public SiteSettingElement this[int index]
    {
        get { return (SiteSettingElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }       // end of public SiteElement this[int index]

    protected override ConfigurationElement CreateNewElement()
    {
        return new SiteSettingElement();
    }       // end of protected override ConfigurationElement CreateNewElement()

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((SiteSettingElement)element).name;
    }
}           // end of public class SiteCollection : ConfigurationElementCollection

最后,我添加了设置元素定义

public class SiteSettingElement : ConfigurationElement
{
    public SiteSettingElement() { }

    [ConfigurationProperty("name", IsRequired=true, IsKey=true)]
    public String name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }           // end of public String name

    [ConfigurationProperty("value", IsRequired = true)]
    public String value
    {
        get { return (String)this["value"]; }
        set { this["value"] = value; }
    }           // end of public String value
}               // end of public class SiteSettingElement : ConfigurationElement

在这一点上,我只是对两个捆绑包重复相同的操作。最后,这一切都有效,并允许可选的设置和部分。

于 2016-05-24T14:10:00.917 回答