2
嗨Retriving信息我有一個配置文件作爲從C#自定義配置文件
<environments selectedEnvironment="10" selectedSite="UK">
<environment env="10">
<secure>
<site name="UK" url="https://www.google.co.uk/"/>
<site name="US" url="https://www.google.com/"/>
</secure>
<content>
<site name="UK" isoCode="UK" url="http://www.yahoo.co.uk"/>
<site name="US" isoCode="UK" url="http://www.yahoo.com"/>
</content>
<office url="http://google-office.com"/>
</environment>
<environment env="24">
<secure>
<site name="UK" url="https://yahoo.co.uk"/>
<site name="US" url="https://yahoo.com"/>
</secure>
<content>
<site name="UK" url="https://google.co.uk"/>
<site name="US" url="https://google.com"/>
</content>
<office url="http://yahoo-docs.com"/>
</environment>
</environments>
我想根據selectedEnvironment和selectedSite
我的電流值來選擇安全,內容和辦公室的網址該代碼是:
部分:
public class EnvironmentsConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("selectedEnvironment", IsRequired = true)]
public string SelectedEnvironment
{
get { return this["selectedEnvironment"].ToString(); }
set { this["selectedEnvironment"] = value; }
}
[ConfigurationProperty("name", IsRequired = true)]
public string EnvironmentName
{
get { return this["name"].ToString(); }
set { this["name"] = value; }
}
[ConfigurationProperty("selectedSite", IsRequired = true)]
public string SelectedSite
{
get { return this["selectedSite"].ToString(); }
set { this["selectedSite"] = value; }
}
[ConfigurationProperty("environment", IsDefaultCollection = true, Options = ConfigurationPropertyOptions.IsRequired)]
[ConfigurationCollection(typeof(EnvironmentConfigurationElement), AddItemName = "selectedEnv")]
public EnvironmentConfigurationElement EnvironmentConfig
{
get { return this["environment"] as EnvironmentConfigurationElement; }
}
}
ElementColle ction:
public class EnvironmentConfigurationCollection : ConfigurationElementCollection
{
public new EnvironmentConfigurationElement this[string key]
{
get { return this.BaseGet(key) as EnvironmentConfigurationElement; }
}
public EnvironmentConfigurationElement this[int index]
{
get { return base.BaseGet(index) as EnvironmentConfigurationElement; }
set
{
// Remove any existing element
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new EnvironmentConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((EnvironmentConfigurationElement)element).Environment;
}
}
元素:
public class EnvironmentConfigurationElement:ConfigurationElement
{
[ConfigurationProperty("env", IsRequired = true, IsKey = true)]
public string Environment
{
get { return this["env"].ToString(); }
set { this["env"] = value; }
}
[ConfigurationProperty("secure", IsRequired = true)]
[ConfigurationCollection(typeof(CheckoutConfigurationCollection), AddItemName ="site")]
public CheckoutConfigurationCollection Secure
{
get { return this["secure"] as SecureConfigurationCollection; }
}
[ConfigurationProperty("content", IsRequired = true)]
[ConfigurationCollection(typeof(ContentConfigurationCollection), AddItemName = "site")]
public CheckoutConfigurationCollection Content
{
get { return this["content"] as CheckoutConfigurationCollection; }
}
[ConfigurationProperty("office", IsRequired = true)]
[ConfigurationCollection(typeof(officeBaseUrlConfigurationElement), AddItemName = "url")]
public BackOfficeBaseUrlConfigurationElement Office
{
get { return this["office"] as officeBaseUrlConfigurationElement; }
}
}
當我運行使用
EnvironmentConfiguration = (EnvironmentsConfigurationSection)ConfigurationManager.GetSection("testExecutionSettings/environments");
我得到屬性的錯誤代碼 「ENV」 沒有找到,即使它的部分目前的元素..
任何人都可以告訴我在這種情況下做什麼?
你能發佈確切的錯誤你得到什麼?包含異常類型,完整消息和堆棧。 – fsimonazzi