写自己的配置文件中的section

来源:互联网 发布:r语言mac版 编辑:程序博客网 时间:2024/05/20 18:02

VS2013 控制台程序

配套代码:http://download.csdn.net/detail/u010476739/9598947

步骤一、确定自己的配置文件最终形式

如:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="jacksection" type="ConsoleApplication2.MySectionHandler,ConsoleApplication2" />
  </configSections>
  <jacksection>
    <MyAdd name="ji" value="de" />
    <MyAdd name="ui" value="yh" />
  </jacksection>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

步骤二、引用程序集:System.Configuration

步骤三、添加类MySectionSetting

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;


namespace ConsoleApplication2
{
    public class MySectionSetting
    {
        internal static NameValueCollection props=new NameValueCollection();        


        public static NameValueCollection Properties
        {
            get
            {
                return props;
            }
        }
    }
}

步骤四、添加类MySectionHandler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
using System.Xml.XPath;


namespace ConsoleApplication2
{
    public class MySectionHandler:IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            NameValueCollection kv = new NameValueCollection();
            XmlNodeList list= section.SelectNodes("MyAdd");
            if (list != null)
            {
                foreach (XmlNode node in list)
                {
                    XmlAttribute attrName=node.Attributes["name"];
                    XmlAttribute attrValue=node.Attributes["value"];
                    if (attrName == null || attrValue == null)
                    {
                        continue;
                    }
                    kv.Add(attrName.Value, attrValue.Value);
                }
            }
            MySectionSetting.props = kv;
            return null;            
        }
    }
}

步骤五、进行测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Test2();
            Console.WriteLine("ok");
            Console.ReadLine();
            
        }


        private static void Test2()
        {
            ConfigurationManager.GetSection("jacksection");
            NameValueCollection kv = MySectionSetting.Properties;
            string[] keys=kv.AllKeys;
            foreach (var key in keys)
            {
                Console.WriteLine(key + "--" + kv[key]);
            }
        }
    }
}

步骤六、查看结果



注意:步骤五中的代码:ConfigurationManager.GetSection("jacksection");非常重要,只有调用这个代码,CLR才会去解析


1 0
原创粉丝点击