【问题标题】:Reading sections in app.config issue阅读 app.config 问题中的部分
【发布时间】:2014-09-28 04:56:43
【问题描述】:

我的 app.config 分为以下几个部分:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="databaseConnectionStrings" type="System.Configuration.NameValueSectionHandler" />
    <section name="dataDictionary" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <databaseConnectionStrings>
    <add key="ILFSsqlServer" value="ODBC;DSN=sql server copycloas;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=ILFSView;"/>
  </databaseConnectionStrings>
  <dataDictionary>
    <add key="CLOASEUCDBA_T_CONTACT" value="CLOASEUCDBA.T_Contact" />
  </dataDictionary>
</configuration>

据我所知,xml 应该可以正常工作,但是当我尝试使用以下方式调用它时:

var section2 = ConfigurationManager.GetSection("dataDictionary") as NameValueConfigurationCollection;
var result2 = section2["CLOASEUCDBA_T_CONTACT"];
Console.WriteLine(result2);

我在第二行得到一个空引用异常。我不知道为什么它不应该为空......

【问题讨论】:

  • 您将问题命名为“已解决”,这是否意味着您发现了问题所在?如果是这样,请接受我的回答,或者如果我的回答没有帮助,请自己写。 =) 这将有助于其他社区成员找到类似问题的相关解决方案。
  • 我已经编辑了您的问题以从中删除答案,因为您创建了一个单独的答案。

标签: c# xml app-config


【解决方案1】:

我看到你将ConfigurationManager.GetSection("dataDictionary") 转换为NameValueConfigurationCollection。将转换为System.Collections.Specialized.NameValueCollection

    [Test]
    public void Test_NameValueConfigurationSection()
    {
        var section2 = ConfigurationManager.GetSection("dataDictionary") as NameValueCollection;
        var result2 = section2["CLOASEUCDBA_T_CONTACT"];

        Assert.AreEqual("CLOASEUCDBA.T_Contact", result2);
    }

【讨论】:

    【解决方案2】:

    通过使用NameValueCollection而不是NameValueConfigurationCollection解决

    【讨论】:

      【解决方案3】:

      看来您必须将类型转换为AppSettingsSection 而不是NameValueCollection

      来自 MSDN:ConfigurationManager.GetSection Method

      // Create the AppSettings section. 
      // The function uses the GetSection(string)method  
      // to access the configuration section.  
      // It also adds a new element to the section collection. 
      public static void CreateAppSettings() {
          // Get the application configuration file.
          System.Configuration.Configuration config =
              ConfigurationManager.OpenExeConfiguration(
                  ConfigurationUserLevel.None);
      
          string sectionName = "appSettings";
      
          // Add an entry to appSettings. 
          int appStgCnt =
              ConfigurationManager.AppSettings.Count;
          string newKey = "NewKey" + appStgCnt.ToString();
      
          string newValue = DateTime.Now.ToLongDateString() + " " 
                                + DateTime.Now.ToLongTimeString();
      
          config.AppSettings.Settings.Add(newKey, newValue);
      
          // Save the configuration file.
          config.Save(ConfigurationSaveMode.Modified);
      
          // Force a reload of the changed section. This  
          // makes the new values available for reading.
          ConfigurationManager.RefreshSection(sectionName);
      
          // Get the AppSettings section.
          AppSettingsSection appSettingSection =
              (AppSettingsSection)config.GetSection(sectionName);
      
          Console.WriteLine();
          Console.WriteLine("Using GetSection(string).");
          Console.WriteLine("AppSettings section:");
          Console.WriteLine(
          appSettingSection.SectionInformation.GetRawXml());
      }
      

      注意Console.WriteLine() 之前的 config.GetSection() 方法调用。

      我经常使用的另一种方法是访问项目属性,如下所示。

      using Namespace.Properties;
      
      public class MyClass {
          public string MySettingMeaningfulName { 
              get { return Settings.Default.ClOASEUCDBA_T_CONTACT; }
          }
      }
      

      【讨论】:

      • 我可能在这里遗漏了一些东西,但这只是在运行时创建 app.config 而没有被保存吗?
      • 已保存。看到这一行:“//保存配置文件。”,后跟config.Save(ConfigurationSaveMethod.Modified);?
      猜你喜欢
      • 1970-01-01
      • 2011-05-25
      • 2012-06-26
      • 2014-08-19
      • 2014-10-31
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多