gpt4 book ai didi

c# - 从 C# 自定义配置文件中检索信息

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:58 26 4
gpt4 key购买 nike

您好,我有一个配置文件作为

<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 的值选择 secure、content 和 office 中的 url

我目前的代码是:

部分:

 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; }
}
}

元素集合:

 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”没有找到,即使它作为元素的一部分存在..

谁能告诉我在这种情况下该怎么办?

最佳答案

如果您愿意尝试 Linq2Xml 和 XPath

string selectedEnvironment="10";
string selectedSite= "UK";

var xpath = string.Format("//environments[@selectedEnvironment='{0}' and @selectedSite='{1}']//*[@url]",
selectedEnvironment,
selectedSite);

var xDoc = XDocument.Parse(xml); //XDocument.Load(filename)

var urls = xDoc.XPathSelectElements(xpath)
.Select(e => e.Attribute("url").Value)
.ToList();

关于c# - 从 C# 自定义配置文件中检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13730710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com