我是c#的新手,我有一些要求我需要读取以下App.config文件的值。
要求1。 在配置节点中,我需要计算ArchivePath节点的数量并循环 他们每个人。假设我需要遍历第一个ArchivePath节点,需要访问Email,Name和SSN的值。就像我需要遍历所有这些值一样。
2.我还需要访问Key LocationName并从节点获取值。
我真的不是那么多c#专家,我使用.net 4.0
<configuration>
<Config>
<ArchivePath Email="Y" Name="ClientName" SSN="123455678"/>
<ArchivePath Email="Y" Name="ClientName1" SSN="123455678"/>
<ArchivePath Email="Y" Name="ClientName2" SSN="123455678"/>
</Config>
<appSettings>
<!--all times in seconds-->
<add key="LocationName" value="India" />
</appSettings>
</configuration>
先谢谢。
我尝试了以下链接中的代码 http://blog.danskingdom.com/adding-and-accessing-custom-sections-in-your-c-app-config/
我从下面的课程收到错误, 代码中是否缺少任何内容
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication2
{
public class ConnectionManagerDataSection : ConfigurationSection
{
/// <summary>
/// The name of this section in the app.config.
/// </summary>
public const string SectionName = "ConnectionManagerDataSection";
private const string EndpointCollectionName = "ConnectionManagerEndpoints";
[ConfigurationProperty(EndpointCollectionName)]
[ConfigurationCollection(typeof(ConnectionManagerEndpointsCollection), AddItemName = "add")]
public ConnectionManagerEndpointsCollection ConnectionManagerEndpoints { get { return (ConnectionManagerEndpointsCollection)base[EndpointCollectionName]; } }
}
public class ConnectionManagerEndpointsCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ConnectionManagerEndpointElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ConnectionManagerEndpointElement)element).Name;
}
}
public class ConnectionManagerEndpointElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("address", IsRequired = true)]
public string Address
{
get { return (string)this["address"]; }
set { this["address"] = value; }
}
[ConfigurationProperty("useSSL", IsRequired = false, DefaultValue = false)]
public bool UseSSL
{
get { return (bool)this["useSSL"]; }
set { this["useSSL"] = value; }
}
[ConfigurationProperty("securityGroupsAllowedToSaveChanges", IsRequired = false)]
public string SecurityGroupsAllowedToSaveChanges
{
get { return (string)this["securityGroupsAllowedToSaveChanges"]; }
set { this["securityGroupsAllowedToSaveChanges"] = value; }
}
}
}
var connectionManagerDataSection = ConfigurationManager.GetSection(ConnectionManagerDataSection.SectionName) as ConnectionManagerDataSection;
if (connectionManagerDataSection != null)
{
foreach (ConnectionManagerEndpointElement endpointElement in connectionManagerDataSection.ConnectionManagerEndpoints)
{
var endpoint = new ConnectionManagerEndpoint() { Name = endpointElement.Name, ServerInfo = new ConnectionManagerServerInfo() { Address = endpointElement.Address, UseSSL = endpointElement.UseSSL, SecurityGroupsAllowedToSaveChanges = endpointElement.SecurityGroupsAllowedToSaveChanges.Split(',').Where(e => !string.IsNullOrWhiteSpace(e)).ToList() } };
AddEndpoint(endpoint);
}
}
答案 0 :(得分:0)