Ok, here you go :)
First off, you need to have a <configSections> tag. This tag will contain
child tags named <section>
<configSections>
<section name="MyWebAppV1" type="MyWebAppV1.V1ConfigSection, MyWebAppV1"
/>
<section name="MyWebAppV2" type="MyWebAppV1.V2ConfigSection, MyWebAppV1"
/>
</configSections>
Then, you will create a tag with the name of the section that you named in
the configSections.
<MyWebAppV1>
<MachineName>XENON</MachineName>
<SiteUrl>/blog/</SiteUrl>
</MyWebAppV1>
<MyWebAppV2>
<MachineName>XENON - V2</MachineName>
<SiteUrl>/blog/v2/</SiteUrl>
</MyWebAppV2>
Now, you are probably wondering about how you can access this information,
right? Well, it's easy. You need to create a class that inherits from
IConfigurationSectionHandler and a config class that matches the layout of
your custom section.
using System.Xml;
using System.Xml.Serialization;
namespace MyWebAppV1
{
public class V1ConfigSection: IConfigurationSectionHandler
{
public object Create(
object parent,
object configContext,
XmlNode section )
{
MyWebAppV1Config config;
XmlSerializer serializer = new
XmlSerializer(typeof(MyWebAppV1Config));
XmlNodeReader reader = new XmlNodeReader(section);
config = (MyWebAppV1Config)serializer.Deserialize(reader);
reader.Close();
return config;
}
}
[Serializable]
[XmlType]
public class MyWebAppV1Config
{
[XmlElement]
public string MachineName;
[XmlElement]
public string SiteUrl;
}
}
Finally, to call your custom config section, you use the
System.Configuration namespace.
MyWebAppV1Config config =
(MyWebAppV1Config)ConfigurationSettings.GetConfig( "MyWebAppV1");
You can also use the ConfigurationSettings.GetConfig by itself, but I have
found that doing it this way is a lot nicer. Now, there are all sorts of
ways that you can go forward with this. I would start by reading up on Xml
Serialization and what you can do with it.
HTH,
Kyril
"Nick Gilbert" <ne**@nickgilbert.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi,
I would like the ability to store the configuration settings for all
versions of my site in a single web.config file by using different
sections. Eg:
<siteConfig>
<machine name="XENON">
<site url="/blog/">
<mode value="live">
<add key="ConnectionString" value="blah" /> <add key="WebmasterEmail"
value="fo*@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah" /> <add key="WebmasterEmail"
value="fo*@bar.com" />
</mode>
</site>
<site url="/intranet/">
<mode value="live">
<add key="ConnectionString" value="blah2" /> <add key="WebmasterEmail"
value="fr**@bar.com" />
</mode>
<mode value="dev">
<add key="ConnectionString" value="blah2" /> <add key="WebmasterEmail"
value="fr**@bar.com" />
</mode>
</site>
</machine>
<machine name="NEON">
...(variation on above)
</machine>
</siteConfig>
How can I write a configuration section handler that will allow me to do
this? There are some examples on the web but most of them don't have any
attributes on the custom section names, so I'm not sure it's possible to
do what I want.
If it's possible to retrieve the values somehow using the structure above,
then it will be simple for me to write the code that accesses the correct
nodes based on things like Server.MachineName and Request.Url etc.
If anyone has seen any code which might help me, please let me know.
Thanks,
Nick...