在app.config问题中阅读部分

时间:2014-08-05 13:44:28

标签: c# xml app-config

我的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);

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

3 个答案:

答案 0 :(得分:1)

您似乎必须输入强制转换为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());
}

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

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

using Namespace.Properties;

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

答案 1 :(得分:1)

使用NameValueCollection代替NameValueConfigurationCollection

解决

答案 2 :(得分:0)

我看到你正在向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);
    }