C# app.config的自定义配置-节集合?

C# app.config的自定义配置-节集合?,c#,.net,app-config,C#,.net,App Config,我的头痛得要命!我以前做过这件事,但没有这么“深入”或复杂,我尝试过不同的方法来实现这一点,但都失败了 这是我想要在app.config中使用的自定义XML: <Profiles> <!--Collection--> <Profile Name="Live"> <Components> <Component Name="Portal" Type="Web" /> <Component Name="Comms"

我的头痛得要命!我以前做过这件事,但没有这么“深入”或复杂,我尝试过不同的方法来实现这一点,但都失败了

这是我想要在app.config中使用的自定义XML:

<Profiles> <!--Collection-->
<Profile Name="Live">
   <Components>
    <Component Name="Portal" Type="Web" />
    <Component Name="Comms" Type="Web" />
    <Component Name="Scheduler" Type="WindowsService" ServiceName="LiveScheduler" />
  </Components>
  <Databases>
    <Database Name="Main" Connection="Data Source=.\SQL2008" />
    <Database Name="Staging" Connection="Data Source=SomeSite.co.uk" />
  </Databases>
</Profile>
<Profile Name="Test">
   <Components>
    <Component Name="Portal" Type="Web" />
    <Component Name="Comms" Type="Web" />
    <Component Name="Scheduler" Type="WindowsService" ServiceName="TestScheduler" />
  </Components>
  <Databases>
    <Database Name="Main" Connection="Data Source=.\SQL2008" />
    <Database Name="Staging" Connection="Data Source=Internal" />
  </Databases>
</Profile>
</Profiles>

除了需要一个额外的级别外,您的实现是正确的。将配置文件从ConfigurationSection更改为ConfigurationElement,然后创建包含配置文件项集合的ConfigurationSection配置文件


这对于自动化测试来说是一个很好的例子,调试配置部分而不进行调试是一件非常痛苦的事情。

您需要将配置部分提高一个级别

public class Profiles : ConfigurationSection
{
    [ConfigurationProperty("Profile")]
    public ProfileCollection Profile
    {
        get
        {
            return this["profile"] as ProfileCollection;
        }
    }
}    
这是我创建的一个部分。您应该能够通过以下方式工作:

public class ImportConfiguration : ConfigurationSection
{
    [ConfigurationProperty("importMap")]
    public ImportMapElementCollection ImportMap
    {
        get
        {
            return this["importMap"] as ImportMapElementCollection;
        }
    }
}

public class ImportColumnMapElement : ConfigurationElement
{
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
    public string LocalName
    {
        get
        {
            return this["localName"] as string;
        }
        set
        {
            this["localName"] = value;
        }
    }

    [ConfigurationProperty("sourceName", IsRequired = true)]
    public string SourceName
    {
        get
        {
            return this["sourceName"] as string;
        }
        set
        {
            this["sourceName"] = value;
        }
    }
}

public class ImportMapElementCollection : ConfigurationElementCollection
{
    public ImportColumnMapElement this[object key]
    {
        get
        {
            return base.BaseGet(key) as ImportColumnMapElement;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "columnMap";
        }
    }

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if (!String.IsNullOrEmpty(elementName))
            isName = elementName.Equals("columnMap");
        return isName;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ImportColumnMapElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImportColumnMapElement)element).LocalName;
    }
}

看起来你正试图为不同的环境提供不同的配置文件,为什么不看看本周发布的VS的慢速Cheetah扩展呢。这基本上就是你想要的:关于代码问题的任何信息?谢谢大家迄今为止的回复。猎豹分机看起来很有趣。呜呜。。按enter键太快。ssamuel-我相信我以前尝试过您的方法,但除了抛出异常说它需要实现IConfigurationSectionHandler之外,没有其他方法。我会看看能不能再做一次。但我想知道将一个对象序列化/反序列化为XML并使用它是否会更快:老实说,您使用的是什么版本的FW?IConfigurationSectionHandler在2.0之后不推荐使用。如果你还在找。序列化为XML更快、更直观。写入配置更漂亮。这取决于您希望将维护的困难留给系统管理员或开发人员。
public class ImportConfiguration : ConfigurationSection
{
    [ConfigurationProperty("importMap")]
    public ImportMapElementCollection ImportMap
    {
        get
        {
            return this["importMap"] as ImportMapElementCollection;
        }
    }
}

public class ImportColumnMapElement : ConfigurationElement
{
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
    public string LocalName
    {
        get
        {
            return this["localName"] as string;
        }
        set
        {
            this["localName"] = value;
        }
    }

    [ConfigurationProperty("sourceName", IsRequired = true)]
    public string SourceName
    {
        get
        {
            return this["sourceName"] as string;
        }
        set
        {
            this["sourceName"] = value;
        }
    }
}

public class ImportMapElementCollection : ConfigurationElementCollection
{
    public ImportColumnMapElement this[object key]
    {
        get
        {
            return base.BaseGet(key) as ImportColumnMapElement;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "columnMap";
        }
    }

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if (!String.IsNullOrEmpty(elementName))
            isName = elementName.Equals("columnMap");
        return isName;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ImportColumnMapElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImportColumnMapElement)element).LocalName;
    }
}