Моя голова сильно колотится! Я делал это раньше, но не настолько глубоко или сложно, как это, и я пробовал разные способы, чтобы это произошло, но все не удалось.
Итак, это пользовательский XML, который я хочу в app.config:
<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>
Таким образом, коллекция Profile с каждым профилем, имеющим коллекцию подэлементов (Components - это коллекция, затем Component - это элемент)
Однако в настоящее время у меня есть все, КРОМЕ для нескольких профилей. Я вроде как вижу проблему, но не знаю, как ее «исправить».
Код:
public class Profile : ConfigurationSection
{
[ConfigurationProperty("Name", IsRequired=true)]
public string Name
{
get
{
return base["Name"] as string;
}
set
{
base["Name"] = value;
}
}
[ConfigurationProperty("Components")]
public ComponentCollection Components
{
get { return ((ComponentCollection)(base["Components"])); }
}
[ConfigurationProperty("Databases")]
public DatabaseCollection Databases
{
get
{
return ((DatabaseCollection)(base["Databases"]));
}
}
}
[ConfigurationCollection(typeof(Component), AddItemName="Component")]
public class ComponentCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Component();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Component)(element)).Name;
}
public Component this[int idx]
{
get
{
return base.BaseGet(idx) as Component;
}
set
{
if (base.BaseGet(idx) != null)
{
base.BaseRemoveAt(idx);
}
this.BaseAdd(idx, value);
}
}
public Component this[string key]
{
get
{
return base.BaseGet(key) as Component;
}
set
{
if (base.BaseGet(key) != null)
{
base.BaseRemove(key);
}
this.BaseAdd(this.Count, value);
}
}
}
public class Component : ConfigurationElement
{
[ConfigurationProperty("Type", IsRequired = true)]
public string Type
{
get
{
return this["Type"] as string;
}
set
{
this["Type"] = value;
}
}
[ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return this["Name"] as string;
}
set
{
this["Name"] = value;
}
}
[ConfigurationProperty("ServiceName", IsRequired = false)]
public string ServiceName
{
get
{
return this["ServiceName"] as string;
}
set
{
this["ServiceName"] = value;
}
}
}
[ConfigurationCollection(typeof(Database), AddItemName = "Database")]
public class DatabaseCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Database();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Database)(element)).Name;
}
public Database this[int idx]
{
get
{
return base.BaseGet(idx) as Database;
}
set
{
if (base.BaseGet(idx) != null)
{
base.BaseRemoveAt(idx);
}
this.BaseAdd(idx, value);
}
}
public Database this[string key]
{
get
{
return base.BaseGet(key) as Database;
}
set
{
if (base.BaseGet(key) != null)
{
base.BaseRemove(key);;
}
this.BaseAdd(this.Count, value);
}
}
}
public class Database : ConfigurationElement
{
[ConfigurationProperty("Name", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return this["Name"] as string;
}
set
{
this["Name"] = value;
}
}
[ConfigurationProperty("Connection", IsKey = false, IsRequired = true)]
public string Connection
{
get
{
return this["Connection"] as string;
}
set
{
this["Connection"] = value;
}
}
}