如何在web.config文件中自定义xml 节点

cs文件: 

public class Log : LogDataService
    {
        /// <summary>
        /// key is envrionment name, value is envrionment path
        /// </summary>
        /// <returns></returns>
        protected override Dictionary<string, string> GetEnvironment()
        {
            //NameValueCollection keyValues = ConfigurationManager.AppSettings;
            //Dictionary<string, string> dicNameAndPath = new Dictionary<string, string>();
            //foreach (var item in keyValues.AllKeys)
            //{
            //    dicNameAndPath.Add(item, keyValues[item]);
            //}
            //return dicNameAndPath;
            Dictionary<string, string> dicNameAndPath = new Dictionary<string, string>();
            List<Environment> lstEnvironment = ConfigurationManager.GetSection("EnvironmentList") as List<Environment>;
            foreach (Environment info in lstEnvironment)
            {
                dicNameAndPath.Add(info.EnvironmentName, info.EnvironmentPath);
            }
            return dicNameAndPath;
        }

        protected override string GetConfigPath()
        {
            return HttpContext.Current.Server.MapPath("~/App_Data/Config/Log/");
        }
    }

    public class Environment
    {
        public Environment(XmlNode siteinfo)
        {
            XmlElement info = (XmlElement)siteinfo;
            this.EnvironmentName = info.SelectSingleNode("EnvironmentName").InnerText;
            this.EnvironmentPath = info.SelectSingleNode("EnvironmentPath").InnerText;
        }

        public string EnvironmentName { get; set; }

        public string EnvironmentPath { get; set; }
    }

    public class ConvertCfgHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            List<Environment> lstEnvironment = new List<Environment>();

            foreach (XmlNode xmlEnvironment in section.SelectNodes("//Environment"))
            {
                Environment environment = new Environment(xmlEnvironment);
                lstEnvironment.Add(environment);
            }

            return lstEnvironment;
        }
    }

web.config文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="EnvironmentList" type="FuturMaster.Web.Services.ConvertCfgHandler,FuturMaster.Web.WebApp"/>
  </configSections>
  <EnvironmentList>
    <Environment>
      <EnvironmentName>Product</EnvironmentName>
      <EnvironmentPath>Z:\ZhiqiangXu\</EnvironmentPath>
    </Environment>
    <Environment>
      <EnvironmentName>Test</EnvironmentName>
      <EnvironmentPath>C:\FMRobotLog\Script and Log\</EnvironmentPath>
    </Environment>
    <Environment>
      <EnvironmentName>Release</EnvironmentName>
      <EnvironmentPath>E:\FMWork\BatchLogs\</EnvironmentPath>
    </Environment>
    <Environment>
      <EnvironmentName>Samples</EnvironmentName>
      <EnvironmentPath>M:\Buny\FMLogServer\Requirement\Samples\Logs and scripts\</EnvironmentPath>
    </Environment>
  </EnvironmentList>
  <location path="FMBiz4Browser.svc">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

  <system.web>
  </system.web>
</configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值