如何从* .config获取不是app.config的部分

时间:2017-06-29 05:58:59

标签: c#

我的问题是如何在c#中访问不是app.config文件的* .config文件。我想阅读此示例文件的部分和值​​。我在项目中将该文件保存为system.config。我不想手动解析该xml文件。有没有直接获取节值的选项?我真的很新c#所以我希望你的帮助。感谢。

    <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <configSections>
             <sectionGroup name="common">
               <section name="system"   
                    type="Common.Logging.ConfigurationSectionHandler, 
                    Common.Logging" />
             </sectionGroup>
           </configSections>

      <common>
         <system>
           <factoryAdapter type="ConsoleApplication.Log4NetFactoryAdapter, 
            ConsoleApplication" >
            <arg key="configType" value="EXTERNAL" />
            <arg key="configFile" value="log4net.config" />
           </factoryAdapter>
          </system>
       </common>

       <startup>
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" 
            />
       </startup>

       <runtime>
           <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
           <dependentAssembly>
           <assemblyIdentity name="log4net" 
            publicKeyToken="669e0ddf0bb1aa2a" 
            culture="neutral" />
           <bindingRedirect oldVersion="0.0.0.0-2.0.8.0" 
            newVersion="2.0.8.0" />
           </dependentAssembly>
          </assemblyBinding>
     </runtime>

     </configuration>

3 个答案:

答案 0 :(得分:1)

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap {ExeConfigFilename = @"..\YourProjectDir\system.config"};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

ConfigurationSectionGroup sectionGroup = config.GetSectionGroup("common");
ClientSettingsSection section = (ClientSettingsSection) sectionGroup.Sections.Get("system");
SettingElement setting = section.Settings.Get("your_setting");

答案 1 :(得分:0)

    var sect = ConfigurationManager.GetSection("common") as NameValueCollection;
var value = sect["configType"];

答案 2 :(得分:0)

好的我修好了。这是我的解决方案:

                        string file = fileName;
                        System.Xml.XmlDocument xDoc = new 
                        System.Xml.XmlDocument();
                        NameValueCollection nameValueColl = new 
                        NameValueCollection();

                        System.Configuration.ExeConfigurationFileMap map = 
                        new ExeConfigurationFileMap();
                        map.ExeConfigFilename = file;
                        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                        string xml = config.GetSection(sectionName).SectionInformation.GetRawXml();
                        xDoc.LoadXml(xml);

                        System.Xml.XmlNode fatherNode = xDoc.ChildNodes[0];
                        System.Xml.XmlNode fathersChildNode = fatherNode.ChildNodes[0];

                        System.Xml.XmlNode ChildrensChildNode_1 = fathersChildNode.ChildNodes[0];
                        System.Xml.XmlAttribute attribute_1_1 = ChildrensChildNode_1.Attributes[0];
                        System.Xml.XmlAttribute attribute_1_2 = ChildrensChildNode_1.Attributes[1];
                        string attribute_1_key = attribute_1_1.Value;
                        string attribute_1_value = attribute_1_2.Value;

                        System.Xml.XmlNode ChildrensChildNode_2 = fathersChildNode.ChildNodes[1];
                        System.Xml.XmlAttribute attribute_2_1 = ChildrensChildNode_2.Attributes[0];
                        System.Xml.XmlAttribute attribute_2_2 = ChildrensChildNode_2.Attributes[1];
                        string attribute_2_key = attribute_2_1.Value;
                        string attribute_2_value = attribute_2_2.Value;

                        Common.Logging.Configuration.NameValueCollection coll = new Common.Logging.Configuration.NameValueCollection();
                        coll.Add(attribute_1_key, attribute_1_value);
                        coll.Add(attribute_2_key, attribute_2_value);

                        Type type = typeof(Log4NetFactoryAdapter);

                        Common.Logging.Configuration.LogSetting set = new Common.Logging.Configuration.LogSetting(type, coll);