web-config - Web.Config 自定义配置,用于在集合中保存集合

  显示原文与译文双语对照的内容
0 0

我在 Web.Config 中设置一个自定义配置,使用 <configSections></configSections>.

我希望在 Web.Config 中创建和创建的标记结构如下:

<TwitterCredentialsSectionGroup>
 <TwitterCredentialsSection>
 <accounts>
 <account id="ID1">
 <add key="ConsumerKey" value="..."/>
 <add key="ConsumerSecretKey" value="..."/>
 <add key="AccessToken" value="..."/>
 <add key="AccesstSecretToken" value="..."/>
 </account>
 </accounts>
 </TwitterCredentialsSection>
 </TwitterCredentialsSectionGroup>

我在configSections标签中使用以下标记注册了自定义配置节:

<configSections>
<sectionGroup name="TwitterCredentialsSectionGroup">
 <section name="TwitterCredentialsSection" type="TwitterIntegration.TwitterCredentialsSection"
 allowLocation="true" allowDefinition="Everywhere"/>
</sectionGroup>

这里有 2个不同的集合元素,即:

  1. 帐户( 保存每个 Twitter 帐户密钥)
  2. 帐户( 将保存所有 Twitter 帐户实例/标签)

现在,自定义配置的代码文件如下所示:

namespace TwitterIntegration
{
 public class TwitterCredentialsSection : ConfigurationSection
 {
 [ConfigurationProperty("accounts")]
 public AccountCollection Accounts
 {
 get
 {
 return (AccountCollection)this["accounts"];
 }
 }
 [ConfigurationProperty("account")]
 public AccountElement Account
 {
 get
 {
 return (AccountElement)this["account"];
 }
 }
}
public class TwitterKeyElement : ConfigurationElement
{
 [ConfigurationProperty("key",IsKey=true, IsRequired=true)]
 public String Key
 {
 get
 {
 return (String)this["key"];
 }
 }
 [ConfigurationProperty("value")]
 public String Value
 {
 get
 {
 return (String)this["value"];
 }
 }
}
[ConfigurationCollection(typeof(TwitterKeyElement))]
public class AccountElement : ConfigurationElementCollection
{
 [ConfigurationProperty("id", IsRequired = true, IsKey = true)]
 public string ID
 {
 get
 {
 return (string)this["id"];
 }
 }
 protected override ConfigurationElement CreateNewElement()
 {
 return new TwitterKeyElement();
 }
 protected override object GetElementKey(ConfigurationElement element)
 {
 return ((TwitterKeyElement)element).Key;
 } 
}
[ConfigurationCollection(typeof(AccountElement))]
public class AccountCollection : ConfigurationElementCollection
{
 protected override ConfigurationElement CreateNewElement()
 {
 return new AccountElement();
 }
 protected override object GetElementKey(ConfigurationElement element)
 {
 return ((AccountElement)element).ID;
 }
}

%7D

当我尝试读取 Web.Config 时,使用下面的代码,我得到了一个错误:

TwitterCredentialsSection objtcsection = (ConfigurationManager.GetSection("TwitterCredentialsSectionGroup/TwitterCredentialsSection") as TwitterCredentialsSection);

错误: **Parser 错误消息: 无法识别的元素'账号'。

Source Error: 
Line 19: <TwitterCredentialsSection>
Line 20: <accounts>
Line 21: <account id="ID1"> 
Line 22: <add key="ConsumerKey" value="W0SKMPuXzml2CHGsMSoHZA"/>
Line 23: <add key="ConsumerSecretKey" value="kaAS0CgeQqcTWjTvYsie8Owtl8o6N8hcOclY9bKlu4"/> 
**

请问我的代码有什么问题?

谢谢

时间:原作者:1个回答

0 0

你的代码只需要两个小更改:

  1. 为类中的Accounts 属性添加 ConfigurationCollection属性,以定义无法识别的'账号'元素:

    public class TwitterCredentialsSection : ConfigurationSection
    {
     [ConfigurationProperty("accounts")]
     [ConfigurationCollection(typeof(AccountCollection), AddItemName ="account")]
     public AccountCollection Accounts
     {
     get { return (AccountCollection) this["accounts"]; }
     }
    }
  2. 从同一类移除 Account property property property

    [ConfigurationProperty("account")]
    public AccountElement Account
    {
     get { return (AccountElement) this["account"]; }
    }
原作者:
...