C#XML配置文件自定义部分始终为null

时间:2017-08-16 06:01:13

标签: xml config-files

使用VSTO Outlook插件。我需要将配置参数存储在XML文件中。我正在努力解决一些基本的配置文件加载问题。我希望有一个全新的观点:

customConfiguration.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
      <section name="CDSSettings"  type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <CDSSettings>
    <add key="APIusername" value="myUser" />
    <add key="APIpassword" value="myPassword" />
  </CDSSettings>

    <appSettings>
      <add key="logLevel" value="0" /> 
  </appSettings>
</configuration>

代码

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "customConfiguration.xml";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

AppSettingsSection appSettingsSection = (config.GetSection("appSettings") as AppSettingsSection);
// --> All ok

ConfigurationSection CDSSettings = (ConfigurationSection)config.GetSection("CDSSettings");
// --> How to get the APIusername key?

我有可能避免使用XML解析器或SectionInformation.GetRawXml()吗?

1 个答案:

答案 0 :(得分:1)

无法解释为什么ConfigurationManager.GetSectionconfig.GetSection会返回不同的结果object,在第一种情况下可以转换为NameValueCollection,而DefaultSection in second部分可以转换为public class CDSSettings : ConfigurationSection { [ConfigurationProperty("MyValues")] public KeyValueConfigurationCollection MyValues { get { return (KeyValueConfigurationCollection) this["MyValues"]; } set { this["MyValues"] = value; } } } 1}}。

我建议创建一个自定义部分并使用它:

<section name="CDSSettings"  type="UCAddin.CDSSettings, UCAddin" />
...
<CDSSettings>
  <MyValues>
    <add key="APIusername" value="myUser" />
    <add key="APIpassword" value="myPassword" />
  </MyValues>
</CDSSettings>

和config看起来像

var CDSSettings = (CDSSettings)config.GetSection("CDSSettings");

检索代码:

public class Credentials : ConfigurationElement
{
    [ConfigurationProperty("login")]
    public string Login
    {
        get { return (string)this["login"]; }
        set { this["login"] = value; }
    }
}

更多 如果是自定义部分,您还可以指定不同类型的字段,例如 你可以有单独的命名元素:

[ConfigurationProperty("credentials")]
public Credentials Credentials
{
    get { return (Credentials) this["credentials"]; }
    set { this["credentials"] = value; }
}

以命名的方式

<CDSSettings>
  <credentials login="testlogin" />
</CDSSettings>

和config看起来像

public class CDSSettings : ConfigurationSection
{

    [ConfigurationProperty("", IsDefaultCollection = true)]
    public KeyValueConfigurationCollection MyValues => 
                (KeyValueConfigurationCollection) this[string.Empty];
}

检查this MSDN article是否有更多可能性

作为AppSettings 您可以将属性注册为默认集合

<CDSSettings>
  <add key="login" value="User" />
</CDSSettings>

并具有App Settings中的参数

var settings = (CDSSettings)config.GetSection("CDSSettings");
settings.MyValues

但是在代码中,这些数据可以从属性访问(如果你没有在类中实现索引器)

from hashlib import sha1
import hmac
import urllib
import time
import requests

oauth_nonce = "40575512181348616041501137897"
oauth_timestamp = str(int(time.time()))
oauth_consumer_key = ""
consumer_secret = ""
oauth_callback = ""

url = 'https://api.twitter.com/oauth/request_token'

test_str = "oauth_nonce="+oauth_nonce+"&oauth_callback="+oauth_callback+"&oauth_signature_method=HMAC-SHA1&oauth_timestamp="+oauth_timestamp+"&oauth_consumer_key="+oauth_consumer_key+"&oauth_version=1.0"

test_str = urllib.quote(test_str, safe='')
base_string = "POST&"+test_str

key = consumer_secret + "&"
hashed = hmac.new(key, base_string, sha1)

demo_str = hashed.digest().encode("base64").rstrip('\n')

oauth_signture = urllib.quote(demo_str, safe='')

print oauth_signture

client = requests.Session()
client.headers.update({'Authorization' : 'OAuth oauth_nonce="40575512181348616041501137897", oauth_timestamp="'+oauth_timestamp+'", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="'+oauth_consumer_key+'", oauth_signature="'+oauth_signture+'",oauth_callback="http%3A%2F%2Flocalhost%3A8069%2Fcallback'})

resp = client.post(url)    
print resp.content