2

嗨,我有一个配置文件

<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 的值选择安全、内容和办公室中的 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”,即使它作为元素的一部分存在。

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

4

1 回答 1

1

如果您愿意尝试 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();
于 2012-12-05T19:36:56.633 回答