ConfigurationManager读取web.config

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <configSections>
    <section name="Person" type="System.Configuration.NameValueSectionHandler"/>
    <!--以NameValue键值/对的形式返回配置节中的信息-->
    <section name="Man" type="System.Configuration.DictionarySectionHandler"/>
    <!--以Dictionary字典键值对的形式返回配置节中的信息-->
    <section name="Name" type="System.Configuration.SingleTagSectionHandler" />
    <!--基础结构。处理 .config 文件中由单个 XML 标记所表示的各配置节。-->
  </configSections>
  <appSettings>
    <add key="Domain" value="xxxxxx"/>
  </appSettings>
  <connectionStrings>
    <add name="ProjectName" connectionString="xxxxxx"/>
  </connectionStrings>

  <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  
  <!---自定义节点-->
  <Person>
    <add key="zhangsan" value="19931112"/>
    <add key="lisi" value="19921111"/>
  </Person>

  <Man>
    <add key="老大" value="f" />
    <add key="老二" value="s" />
    <add key="老三" value="t" />
  </Man>

  <Name one="1" two="2" three="3" four="4" five="5" />
  <!--注意是要单个节SingleTagSectionHandler才能处理,但是无论你索性有多少个也能处理-->
  
</configuration>


                    string text1 = "", text2 = "", text3 = "", text4 = "", text5 = "";
                    //获取appSettings节点数据
                    text1 = ConfigurationManager.AppSettings["Domain"];
                    
                    //获取connectionStrings节点数据
                    text2 = ConfigurationManager.ConnectionStrings["ProjectName"].ToString();

                    //获取以NameValue键值/对的形式返回配置节中的信息
                    NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("Person");
                    foreach (string key in nvc.AllKeys)
                    {
                        text3 += key + ":" + nvc[key];
                    }

                    //获取以Dictionary字典键值对的形式返回配置节中的信息
                    IDictionary dict = (IDictionary)ConfigurationManager.GetSection("Man"); ;
                    foreach (string key in dict.Keys)
                    {
                        text4 += key + ":" + dict[key] + " ";
                    }
                    
                    //获取由单个 XML 标记所表示的各配置节
                    IDictionary dict1 = (IDictionary)ConfigurationManager.GetSection("Name");
                    foreach (string key in dict1.Keys)
                    {
                        text5 += key + ":" + dict1[key];
                    }
                    string ret =
                        string.Format("AppSettings:{0}-ConnectionStrings:{1}-NameValue:{2}-Dictionary:{3}-XML:{4}",
                            text1, text2, text3, text4, text5);
                    //"AppSettings:xxxxxx-ConnectionStrings:xxxxxx-NameValue:zhangsan:19931112lisi:19921111-Dictionary:老三:t老大:f老二:s -XML:three:3five:5one:1two:2four:4"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值