配置系统初始化失败错误,同时从app.config中
我在下面的方式定义配置节在我的app.config加载自定义栏目:配置系统初始化失败错误,同时从app.config中
<?xml version="1.0" encoding="utf-8" ?> <configuration>
<configSections>
<section name="RegisterCompanies"
type="ConfigTest.RegisterCompaniesConfig, ConfigTest"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<RegisterCompanies>
<Companies>
<Company name="Tata Motors" code="Tata"/>
<Company name="Honda Motors" code="Honda"/>
</Companies>
</RegisterCompanies>
</configuration>
要阅读这些信息我已经创建了三个班在这样的方式:RegisterCompaniesConfig类
public class RegisterCompaniesConfig : ConfigurationSection {
public static RegisterCompaniesConfig GetConfig()
{
string path = Path.Combine(Application.StartupPath, "ConfigTest.exe.config");
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;
return serviceSection;
//return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies");
}
[System.Configuration.ConfigurationProperty("Companies")]
public Companies Companies
{
get
{
object o = this["Companies"]; return o as Companies;
}
}
}
然后公司类:
public class Companies : ConfigurationElementCollection {
[System.Configuration.ConfigurationProperty("Company")]
public Company this[int index]
{
get
{
return base.BaseGet(index) as Company;
}
set
{
if (base.BaseGet(index) != null)
{ base.BaseRemoveAt(index); } this.BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Company();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Company)element).Name;
}
}
,最后一个是公司类:
public class Company : ConfigurationElement {
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return this["name"] as string;
}
}
[ConfigurationProperty("code", IsRequired = true)]
public string Code
{
get
{
return this["code"] as string;
}
}
}
后 时,我想通过调用下面的方法来接取部分
var config = RegisterCompaniesConfig.GetConfig();
我得到的错误:配置系统初始化失败 请任何人都看看上面的代码,问题在哪里,它看起来一切都很好....
回答:
刚刚运行您的代码,我收到的错误是“电子字元素<公司>只可在上线这一部分”出现一次:
RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;
这似乎表明,你只能与代码你现在得到的,有一个公司的因素在里面。
在过去,我已经使用了下面没有任何问题:
<?xml version="1.0" encoding="utf-8" ?> <configuration>
<configSections>
<sectionGroup name="Libraries">
<section name="MyLibrary" type="System.Configuration.NameValueSectionHandler,system, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
</sectionGroup>
</configSections>
<Libraries>
<MyLibrary>
<add key="Test" value="Test1"/>
</MyLibrary>
</Libraries>
</configuration>
我已经然后用类似代码访问:
public string GetValue(string configurationKey, string defaultValue) {
NameValueCollection _config = (NameValueCollection)ConfigurationManager.GetSection("Libraries/MyLibrary");
string result = (_config == null) ? null : _config[configurationKey];
return (result == null ? defaultValue : result);
}
如果你不这样做有到有名为“name”和“code”的属性,那么你可以使用上面的代码,否则你可以使用Reflector来了解NameValueCollection的功能和工作方式!
以上是 配置系统初始化失败错误,同时从app.config中 的全部内容, 来源链接: utcz.com/qa/266126.html