首页  编辑  

C#中自定义app.config节点读取

Tags: /C#/文件目录操作/Xml/   Date Created:

app.config 无法识别的配置节 C# 自定义配置,解决方法:

C#中, 在app.config中,增加自定义小节后,如果要读取信息,请首先在app.config中,添加<configSections>,例如下面:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<section name="pushReceiver" type="WechatPush.PushReceiver, WechatPush" />
	</configSections>
	<pushReceiver>
		<item key="生产二部(QY)" user="a" party="70" tag="" />
		<item key="生产三部(SD)" user="b" party="70" tag ="" />
	</pushReceiver> 
</configuration>
App.config节点读取代码:
public class PushReceiver : IConfigurationSectionHandler
{
	public object Create(object parent, object configContext, XmlNode section)
	{
		List<Receiver> myConfigObject = new List<Receiver>();

		foreach (XmlNode childNode in section.ChildNodes)
		{
			if (!"item".Equals(childNode.Name)) continue;

			Receiver receiver = new Receiver();
			foreach (XmlAttribute attrib in childNode.Attributes)
			{
				if (attrib.Name == "key") receiver.key = attrib.Value;
				if (attrib.Name == "user") receiver.user = attrib.Value;
				if (attrib.Name == "party") receiver.party = attrib.Value;
				if (attrib.Name == "tag") receiver.tag = attrib.Value;
			}
			myConfigObject.Add(receiver) ;
		}
		return myConfigObject;
	}
}

public class Receiver
{
	public string key; // 工厂
	public string user;  // 微信推送接收人
	public string party;  // 微信推送接收部门
	public string tag;  // 微信推送接收部门
}
最后,使用的时候如下:
List<Receiver> pushReceivers = System.Configuration.ConfigurationManager.GetSection("pushReceiver") as List<Receiver>;
foreach (Receiver receiver in pushReceivers)
{
	if (factory.Equals(receiver.key))
	{
		return Wehelper.SendText(receiver.user, receiver.party, receiver.tag, agentid, message);
	}
}

另外一种方式:

name为自定义节点的名称,type为自定义节点解析文件的命名空间和自定义节处理程序的类名

1. 在<configSections>节点中注册你所要定义的节点名称及用于处理该节点的配置节处理程序。代码如下:

<configSections>
<section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"/>
</configSections>

2. 在适当的位置添加自定义的节点。代码如下:

<configSections>
        <section name="dbFactory" type="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration"</configSections>

    <dbFactory>
        <default factory="sql"></default>
        <factorys>
            <add name="sql"  assembly="Hello.Data"  class="SqlDbFactory" />
            <add name="oracle" assembly="Hello.Data" class="OracleDbFactory" />             
        </factorys>
    </dbFactory>

 public class DefaultElement : ConfigurationElement
    {
        [ConfigurationProperty("factory")]
        public string Factory
        {
            get
            {
                return this["factory"] as string;
            }
            set
            {
                this["factory"] = value;
            }
        }
    }

public class FactoryElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("assembly")]
        public string Assembly
        {
            get
            {
                return this["assembly"] as string;
            }
            set
            {
                this["assembly"] = value;
            }
        }

        [ConfigurationProperty("class")]
        public string Class
        {
            get
            {
                return this["class"] as string;
            }
            set
            {
                this["class"] = value;
            }
        }
    }
public class FactoryElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FactoryElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FactoryElement)element).Name;
        }

        public FactoryElement this[string name]
        {
            get
            {
                return BaseGet(name) as FactoryElement;
            }
        }
    }

public class DbFactorySection : ConfigurationSection
    {
        [ConfigurationProperty("default")]
        public DefaultElement DefaultFactory
        {
            get
            {
                return this["default"] as DefaultElement;
            }
            set
            {
                this["default"] = value;
            }
        }

        [ConfigurationProperty("factorys")]
        public FactoryElements Factorys
        {
            get
            {
                return this["factorys"] as FactoryElements;
            }

            set
            {
                this["factorys"] = value;
            }
        }
    }