如何从app.config中读取自定义对象列表
本文关键字:读取 自定义 对象 列表 config app | 更新日期: 2023-09-27 18:15:54
我已经在app.config中定义了自定义设置,但我在阅读该节时出现错误。它的工作原理与简单的结构,如阅读只有一个配置文件,但只要我有一个配置文件列表,它失败了一个错误:"未知元素"配置文件"。谁能告诉我我哪里做错了?
这是我的app.config的一部分:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="DTester.Properties.ProfileSection" type="DTester.Settings.ProfileSection, DTester"/>
</configSections>
<DTester.Properties.ProfileSection>
<profiles>
<profile site="test1" urlscheme="http" urldomain="localhost">
<dataSource dataSource="test2" databaseName="test1" dbUserName="test1" dbUserPassword="test1" />
<user userName="test" password="test" TOHSoftwareVersion="8" iOSVersion="3" deviceUDID="12312"/>
</profile>
<profile site="test2" urlscheme="http" urldomain="localhost" >
<dataSource dataSource="test2" databaseName="test2" dbUserName="test2" dbUserPassword="test2" />
<user userName="test" password="test" TOHSoftwareVersion="3" iOSVersion="8" deviceUDID="123122"/>
</profile>
</profiles>
</DTester.Properties.ProfileSection>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
这是我的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace DTester.Settings
{
public class Profile : ConfigurationElement
{
[ConfigurationProperty("profile")]
public ProfileElem profile
{
get
{
return this["profile"] as ProfileElem;
}
set
{
this["profile"] = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace DTester.Settings
{
[ConfigurationCollection(typeof(ProfileSection))]
public class ProfileCollection : ConfigurationElementCollection
{
public Profile this[int index]
{
get
{
return base.BaseGet(index) as Profile;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Profile();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Profile)(element)).profile.site;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace DTester.Settings
{
public class ProfileElem : ConfigurationElement
{
[ConfigurationProperty("site", IsRequired = true)]
public string site
{
get
{
return this["site"] as string;
}
set
{
this["site"] = value;
}
}
[ConfigurationProperty("urlscheme", IsRequired = true)]
public string urlscheme
{
get
{
return this["urlscheme"] as string;
}
set
{
this["urlscheme"] = value;
}
}
[ConfigurationProperty("urldomain", IsRequired = true)]
public string urldomain
{
get
{
return this["urldomain"] as string;
}
set
{
this["urldomain"] = value;
}
}
[ConfigurationProperty("dataSource", IsRequired = true)]
public DataSource dataSource
{
get
{
return this["dataSource"] as DataSource;
}
set
{
this["dataSource"] = value;
}
}
[ConfigurationProperty("user", IsRequired = true)]
public UserConfig user
{
get
{
return this["user"] as UserConfig;
}
set
{
this["user"] = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace DTester.Settings
{
public class DataSource : ConfigurationElement
{
[ConfigurationProperty("dataSource", IsRequired = true)]
public string dataSource
{
get
{
return this["dataSource"] as string;
}
set
{
this["dataSource"] = value;
}
}
[ConfigurationProperty("databaseName", IsRequired = true)]
public string databaseName
{
get
{
return this["databaseName"] as string;
}
set
{
this["databaseName"] = value;
}
}
[ConfigurationProperty("dbUserName", IsRequired = true)]
public string dbUserName
{
get
{
return this["dbUserName"] as string;
}
set
{
this["dbUserName"] = value;
}
}
[ConfigurationProperty("dbUserPassword", IsRequired = true)]
public string dbUserPassword
{
get
{
return this["dbUserPassword"] as string;
}
set
{
this["dbUserPassword"] = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace DTester.Settings
{
public class UserConfig : ConfigurationElement
{
[ConfigurationProperty("userName", IsRequired = true)]
public string userName
{
get
{
return this["userName"] as string;
}
set
{
this["userName"] = value;
}
}
[ConfigurationProperty("password", IsRequired = true)]
public string password
{
get
{
return this["password"] as string;
}
set
{
this["password"] = value;
}
}
[ConfigurationProperty("TOHSoftwareVersion", IsRequired = true)]
public string TOHSoftwareVersion
{
get
{
return this["TOHSoftwareVersion"] as string;
}
set
{
this["TOHSoftwareVersion"] = value;
}
}
[ConfigurationProperty("iOSVersion", IsRequired = true)]
public string iOSVersion
{
get
{
return this["iOSVersion"] as string;
}
set
{
this["iOSVersion"] = value;
}
}
[ConfigurationProperty("deviceUDID", IsRequired = true)]
public string deviceUDID
{
get
{
return this["deviceUDID"] as string;
}
set
{
this["deviceUDID"] = value;
}
}
}
}
我使用这个工具来获得响应https://nconfiggen.codeplex.com/wikipage?title=Example2&referringTitle=Home
下面是修改后的配置部分:<DTesterConfiguration>
<option cmpResWithOacis="true" valRecentRosterPat="false" maxPatCount="1" maxResCount="3" maxDetCount="3" />
<profiles defaultProfile="test1">
<profile site="test1" urlscheme="http" urldomain="localhost">
<dataSource dataSource="test2" databaseName="test1" dbUserName="test1" dbUserPassword="test1" />
<user userName="test" password="test" TOHSoftwareVersion="dfdf" iOSVersion="dfdf" deviceUDID="dfdf"/>
</profile>
<profile site="test2" urlscheme="http" urldomain="localhost" >
<dataSource dataSource="test2" databaseName="test2" dbUserName="test2" dbUserPassword="test2" />
<user userName="test" password="test" TOHSoftwareVersion="dfdf" iOSVersion="dfdf" deviceUDID="dfdfdf"/>
</profile>
</profiles>
</DTesterConfiguration>
代码如下:
public sealed class DTesterConfiguration
{
private static DTesterConfigurationSection _config;
static DTesterConfiguration()
{
_config = ((DTesterConfigurationSection)(global::System.Configuration.ConfigurationManager.GetSection("DTesterConfiguration")));
}
private DTesterConfiguration()
{
}
public static DTesterConfigurationSection Config
{
get
{
return _config;
}
}
}
public sealed partial class DTesterConfigurationSection : System.Configuration.ConfigurationSection
{
[System.Configuration.ConfigurationPropertyAttribute("profiles")]
[System.Configuration.ConfigurationCollectionAttribute(typeof(ProfilesElementCollection.ProfileElement), AddItemName = "profile")]
public ProfilesElementCollection Profiles
{
get
{
return ((ProfilesElementCollection)(this["profiles"]));
}
}
[System.Configuration.ConfigurationPropertyAttribute("option")]
//[System.Configuration.ConfigurationCollectionAttribute(typeof(OptionElement), AddItemName = "option")]
public OptionElement Option
{
get
{
return ((OptionElement)(this["option"]));
}
}
//<option cmpResWithOacis="true" valRecentRosterPat="false" maxPatCount="10" maxResCount="3" maxDetCount="3" />
public sealed partial class OptionElement : System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationPropertyAttribute("cmpResWithOacis", IsRequired = true)]
public bool CmpResWithOacis
{
get
{
return ((bool)(this["cmpResWithOacis"]));
}
set
{
this["cmpResWithOacis"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("valRecentRosterPat", IsRequired = true)]
public bool ValRecentRosterPat
{
get
{
return ((bool)(this["valRecentRosterPat"]));
}
set
{
this["valRecentRosterPat"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("maxPatCount", IsRequired = true)]
public long MaxPatCount
{
get
{
return ((long)(this["maxPatCount"]));
}
set
{
this["maxPatCount"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("maxResCount", IsRequired = true)]
public long MaxResCount
{
get
{
return ((long)(this["maxResCount"]));
}
set
{
this["maxResCount"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("maxDetCount", IsRequired = true)]
public long MaxDetCount
{
get
{
return ((long)(this["maxDetCount"]));
}
set
{
this["maxDetCount"] = value;
}
}
}
public sealed partial class ProfilesElementCollection : System.Configuration.ConfigurationElementCollection
{
[System.Configuration.ConfigurationPropertyAttribute("defaultProfile", IsRequired = true)]
public string DefaultProfile
{
get
{
return ((string)(this["defaultProfile"]));
}
set
{
this["defaultProfile"] = value;
}
}
public ProfileElement this[int i]
{
get
{
return ((ProfileElement)(this.BaseGet(i)));
}
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new ProfileElement();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return ((ProfileElement)(element)).Site;
}
public sealed partial class ProfileElement : System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationPropertyAttribute("site", IsRequired = true)]
public string Site
{
get
{
return ((string)(this["site"]));
}
set
{
this["site"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("urlscheme", IsRequired = true)]
public string Urlscheme
{
get
{
return ((string)(this["urlscheme"]));
}
set
{
this["urlscheme"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("urldomain", IsRequired = true)]
public string Urldomain
{
get
{
return ((string)(this["urldomain"]));
}
set
{
this["urldomain"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("dataSource")]
public DataSourceElement DataSource
{
get
{
return ((DataSourceElement)(this["dataSource"]));
}
}
[System.Configuration.ConfigurationPropertyAttribute("user")]
public UserElement User
{
get
{
return ((UserElement)(this["user"]));
}
}
public sealed partial class DataSourceElement : System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationPropertyAttribute("dataSource", IsRequired = true)]
public string DataSource
{
get
{
return ((string)(this["dataSource"]));
}
set
{
this["dataSource"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("databaseName", IsRequired = true)]
public string DatabaseName
{
get
{
return ((string)(this["databaseName"]));
}
set
{
this["databaseName"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("dbUserName", IsRequired = true)]
public string DbUserName
{
get
{
return ((string)(this["dbUserName"]));
}
set
{
this["dbUserName"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("dbUserPassword", IsRequired = true)]
public string DbUserPassword
{
get
{
return ((string)(this["dbUserPassword"]));
}
set
{
this["dbUserPassword"] = value;
}
}
}
public sealed partial class UserElement : System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationPropertyAttribute("userName", IsRequired = true)]
public string UserName
{
get
{
return ((string)(this["userName"]));
}
set
{
this["userName"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("password", IsRequired = true)]
public string Password
{
get
{
return ((string)(this["password"]));
}
set
{
this["password"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("TOHSoftwareVersion", IsRequired = true)]
public string TOHSoftwareVersion
{
get
{
return ((string)(this["TOHSoftwareVersion"]));
}
set
{
this["TOHSoftwareVersion"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("iOSVersion", IsRequired = true)]
public string IOSVersion
{
get
{
return ((string)(this["iOSVersion"]));
}
set
{
this["iOSVersion"] = value;
}
}
[System.Configuration.ConfigurationPropertyAttribute("deviceUDID", IsRequired = true)]
public string DeviceUDID
{
get
{
return ((string)(this["deviceUDID"]));
}
set
{
this["deviceUDID"] = value;
}
}
}
}
}
}