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 の値に基づいて、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」が見つからないというエラーが表示されます..

この場合、どうすればよいか誰か教えてもらえますか?

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 に答える