简体   繁体   English

C#.Net 4.0-具有属性和节的自定义配置文件

[英]C# .Net 4.0 - Custom Configuration File with Attributes and sections

I know that this topic has been covered in a number of different Stackoverflow articles, and I have read about 30 of them to make sure that what I am doing matches up with those. 我知道许多不同的Stackoverflow文章都涉及这个主题,并且我阅读了其中的约30篇文章,以确保我所做的与这些内容相符。 It is (even fro the .Net 2.0, 3.0, and 4.0 version of the answers) 是(甚至是答案的.Net 2.0、3.0和4.0版本)

I am attempting to create a very simple (at least in my mind) configuration file with custom attributes on the sections, and then optional items within the sections. 我正在尝试创建一个非常简单(至少在我看来)的配置文件,在各节中具有自定义属性,然后在各节中具有可选项。 So, now to the code: 所以,现在到代码:

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

So, what you are looking at is a global Section of type Sites which contains multiple sections (CollectionElementCollections) of type Site . 因此,您正在查看的是Sites类型的全局Section,其中包含Site类型的多个节(CollectionElementCollections)。 Site is defined by custom attributes on the item, as well as optional items within the section itself. 网站由项目中的自定义属性以及此部分本身中的可选项目定义。 So, siteSettings is optional, JavaScriptBundles is optional, and CSSBundles are also optional. 因此,siteSettings是可选的,JavaScriptBundles是可选的,CSSBundles也是可选的。

The C# Code is below: C#代码如下:

For Sites 对于网站

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

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

For Site Collections 对于网站集

[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();
    }
}

For Site Definition 对于网站定义

/**
 * 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

For SiteSettings Definition 对于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

Site Setting Element 网站设置元素

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

I am leaving out the other items for space, but the other parts look the same. 我将其他项目留给了空间,但其他部分看起来相同。 Basically, what is happening is, I am getting 基本上,发生的事情是

Unrecognized attribute 'siteRoot'. 无法识别的属性“ siteRoot”。 Note that attribute names are case-sensitive. 请注意,属性名称区分大小写。

Looking at everything, it appears that I should be fine, however, I think I may be doing too much and missing things. 综观一切,看来我应该没事,但是,我认为我可能做得太多了,缺少了很多东西。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

I have figured out what was wrong with my code. 我已经弄清楚我的代码出了什么问题。 I am going to provide the information below. 我将在下面提供信息。 I used the following article for help on tracking down some of the pieces: How to implement a ConfigurationSection with a ConfigurationElementCollection 我使用以下文章来帮助您跟踪某些内容: 如何使用ConfigurationElementCollection实现ConfigurationSection

I took the entire code base back to nothing and built it up from scratch. 我将整个代码库归零,并从头开始构建它。 The XML is still the same 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>

So, first I started with the Sites Container 所以,首先我从站点容器开始

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 { 

And then I added the SiteCollection for the Collection of Site Elements 然后我为网站元素的集合添加了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

Then I added the definition for the Site with optional values 然后,我为网站定义添加了可选值

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

Next I added the SiteSettings Collection 接下来,我添加了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

And finally, I added the Setting Element Definition 最后,我添加了设置元素定义

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

At this point, I just repeat the same for the two bundles. 在这一点上,我只对两个捆绑重复相同的操作。 In the end this all works, and allows for optional settings and sections. 最后,所有这些都有效,并且允许可选的设置和部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM