app.config 中的自定义配置部分无法读取 xml 数据作为 C# 应用程序中的集合
Custom configuration section in app.config cannot read xml data as collection in C# application
我有如下 class 扩展的 ApplicationSettingsBase
public class SMTPServerConfig : ApplicationSettingsBase
{
private static SMTPServerConfig instance = (SMTPServerConfig)Synchronized(new SMTPServerConfig());
public static SMTPServerConfig Instance { get { return instance; } }
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
public List<SMTPServerConfigItem> SMTPServers
{
get
{
return ((List<SMTPServerConfigItem>)(this["SMTPServers"]));
}
}
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.String)]
public string TestProp
{
get
{
return ((string)(this["TestProp"]));
}
}
}
[Serializable]
public class SMTPServerConfigItem
{
public string SMTPHost { get; set; }
public string SMTPPort { get; set; }
public string SMTPAccount { get; set; }
public string SMTPPassword { get; set; }
}
这是我的 App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
当我使用 SMTPServerConfig.Instance.TestProp 时,它会显示结果为“123123”(这是真实值,这意味着我有正确的配置架构),但 SMTPServerConfig.Instance.SMTPServers.Count 始终为零。我的代码或配置文件哪里错了?
经过一个小时的研究,我在 Whosebug 中发现了一个问题,然后我知道我错在哪里了。我在 app.config 文件中缺少一个 xml 标签。
题:
Trying to create a custom partial settings file
那么现在我将更正我的 App.config 文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<ArrayOfSMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</ArrayOfSMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
添加ArrayOfSMTPServerConfigItem标签后,即可解决此问题。
我有如下 class 扩展的 ApplicationSettingsBase
public class SMTPServerConfig : ApplicationSettingsBase
{
private static SMTPServerConfig instance = (SMTPServerConfig)Synchronized(new SMTPServerConfig());
public static SMTPServerConfig Instance { get { return instance; } }
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
public List<SMTPServerConfigItem> SMTPServers
{
get
{
return ((List<SMTPServerConfigItem>)(this["SMTPServers"]));
}
}
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.String)]
public string TestProp
{
get
{
return ((string)(this["TestProp"]));
}
}
}
[Serializable]
public class SMTPServerConfigItem
{
public string SMTPHost { get; set; }
public string SMTPPort { get; set; }
public string SMTPAccount { get; set; }
public string SMTPPassword { get; set; }
}
这是我的 App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
当我使用 SMTPServerConfig.Instance.TestProp 时,它会显示结果为“123123”(这是真实值,这意味着我有正确的配置架构),但 SMTPServerConfig.Instance.SMTPServers.Count 始终为零。我的代码或配置文件哪里错了?
经过一个小时的研究,我在 Whosebug 中发现了一个问题,然后我知道我错在哪里了。我在 app.config 文件中缺少一个 xml 标签。 题: Trying to create a custom partial settings file
那么现在我将更正我的 App.config 文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<ArrayOfSMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>admin@example.com</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</ArrayOfSMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
添加ArrayOfSMTPServerConfigItem标签后,即可解决此问题。