如何解析的app.config使用ConfigurationManager中? [英] How to parse app.config using ConfigurationManager?

查看:155
本文介绍了如何解析的app.config使用ConfigurationManager中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我的解析app.config文件一定的方法。然后,我被告知,使用ConfigurationManager中是更好更简单。但事实是,我不知道如何与ConfigurationManager中做到这一点。

I was using a certain method for parsing my app.config file. Then I was told that using ConfigurationManager is better and simpler. But the thing is I don't know how to do it with ConfigurationManager.

我原来的代码是这样的:

My original code looked like this:

   XmlNode xmlProvidersNode;
    XmlNodeList xmlProvidersList;
    XmlNodeList xmlTaskFactoriesList;

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("app.config");
    xmlProvidersNode = xmlDoc.DocumentElement.SelectSingleNode("TaskProviders");
    xmlProvidersList = xmlProvidersNode.SelectNodes("TaskProvider");

    foreach (XmlNode xmlProviderElement in xmlProvidersList)
    {
        if (xmlProviderElement.Attributes.GetNamedItem("Name").Value.Equals(_taskProvider))
        {
            xmlTaskFactoriesList = xmlProviderElement.SelectNodes("TaskTypeFactory");
            foreach (XmlNode xmlTaskFactoryElement in xmlTaskFactoriesList)
            {
                if (xmlTaskFactoryElement.Attributes.GetNamedItem("TaskType").Value.Equals(_taskType))
                {
                    taskTypeFactory = xmlTaskFactoryElement.Attributes.GetNamedItem("Class").Value;
                }
            }
        }
    }



什么将使用ConfigurationManager中等价? (因为所有我可以看到的是如何得到钥匙未结点..)

What would be the equivalent using ConfigurationManager? (Because all I can see is how to get keys not nodes..)

感谢

推荐答案

创建一个类继承配置节叫,说, MyConfigSection 。然后你可以使用 ConfigurationManager.GetSection 方法来获得你的 MyConfigSection 类的一个实例。在 ConfigurationManager中将尽一切解析,所以你将有一个强类型的对象一起工作。 这是一个很好的榜样。

Create a class that inherits ConfigurationSection called, say, MyConfigSection. Then you can use the ConfigurationManager.GetSection method to get an instance of your MyConfigSection class. The ConfigurationManager will do all the parsing, so you will have a strongly typed object to work with. Here is an excellent example to follow.

这篇关于如何解析的app.config使用ConfigurationManager中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆