0

我是 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);
    }
}
4

1 回答 1

0

我从下面的 URl 得到了答案

http://www.codeproject.com/Articles/233965/Custom-Configuration-Sections-in-app-config-or-web

于 2013-11-12T15:37:11.910 回答