zoukankan      html  css  js  c++  java
  • C#中的配置文件自定义解析 [转帖]

    以前总是用配置文件以外的xml来进行附加的应用程序配置设置,近期发现原来C#中已提供了相应的方法,不由得又一次强烈鄙视自己的无知.本例为vs2003版本,vs2005中个别方法可能已过期, 不过整体模式相同.

     一,修改配置文件(web.config或App.config)内容

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <!--type中用逗号分割了完整类名称和程序集(*.dll或*.exe)名称,name制定自定义配置的根节点名称-->
        <section name="Sample" type="Sample.ConfigHandler,Sample" />
      </configSections>
      <Sample>
        <myConfig name="sharpnessdotnet" sex="male"/>
      </Sample>
    </configuration>

    二,配置解析类定义

    public class ConfigHandler:System.Configuration.IConfigurationSectionHandler
        {
            object System.Configuration.IConfigurationSectionHandler.Create(object parent, object configContext, System.Xml.XmlNode section)
            {
                XmlNodeList configs = section.SelectNodes("myConfig");
                foreach(XmlNode config in configs)
                {
                    Console.WriteLine(config.Attributes["name"].Value);
                    Console.WriteLine(config.Attributes["sex"].Value);
                }
                return "hello";
            }
        }

    三,客户端测试代码

     class Program
        {
            static void Main(string[] args)
            {          
                object value = ConfigurationSettings.GetConfig("Sample");//此处可返回object,开发人员可以自己去做文章
                Console.WriteLine(value.ToString());
                Console.Read();
            }
        }

    四,输出效果展示

    转自:http://blog.csdn.net/sharpnessdotnet/archive/2008/03/27/2223177.aspx

  • 相关阅读:
    GoogleTest 之路2-Googletest 入门(Primer)
    GoogleTest 之路1-Generic Build Instructions编译指导总方案
    Tinyhttpd 知识点
    栈初始化
    ARM S3C2440 时钟初始化流程
    GNU 关闭 MMU 和 Icache 和 Dcache
    bootloader 关闭看门狗
    bootloader svc 模式
    Uboot S3C2440 BL1 的流程
    GNU 汇编 协处理器指令
  • 原文地址:https://www.cnblogs.com/liangwei389/p/1333348.html
Copyright © 2011-2022 走看看