我正在使用<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个不同的收集元素,即:
现在我的自定义配置代码文件如下:
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;
}
}
}
现在当我尝试阅读web.config时,使用下面的代码,我收到一个错误:
TwitterCredentialsSection objtcsection = (ConfigurationManager.GetSection("TwitterCredentialsSectionGroup/TwitterCredentialsSection") as TwitterCredentialsSection);
错误: **分析程序错误消息:无法识别的元素“帐户”。
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"/>
**
任何人都可以建议我的代码有什么问题吗?
由于
答案 0 :(得分:0)
您的代码只需要进行两处小改动:
将 ConfigurationCollection属性添加到Accounts
课程中的TwitterCredentialsSection
媒体资源,以便定义无法识别的帐户&#39;元素:
public class TwitterCredentialsSection : ConfigurationSection
{
[ConfigurationProperty("accounts")]
[ConfigurationCollection(typeof(AccountCollection), AddItemName = "account")]
public AccountCollection Accounts
{
get { return (AccountCollection) this["accounts"]; }
}
}
从同一个Account
类中移除 TwitterCredentialsSection
属性:
[ConfigurationProperty("account")]
public AccountElement Account
{
get { return (AccountElement) this["account"]; }
}