Dave, Thats certainly one possible approach, especially since the Web.config file is fairly well defined in structure, the draw back is that the Web.config file like any .NET config file is extensible so as well as your <system.web /> element you might have a <123aspx.superapp /> element as well and a hard coded serialiser might have a few issues with this. Tread lightly with configuration files, it is possible to override critical settings which could cripple or worse expose your app.
For reading custom configuration data I implent the IConfigurationSectionHandler interface which you then reference in the .NET config file, here is a minimal implementation of it: using System; using System.Configuraton; using System.Xml; public class MySectionHandler : System.Configuration.IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { // Just return the node. return section; } } What I like to do is create my own ConfigurationSettings class in my application, for example MyAppConfigurationSettings which extends the functionality of the class in the System.Configuration namespace. For example: using System; using System.Configuration; public class MyAppConfigurationSettings { public static NameValueCollection AppSettings { get { // Just call the normal implementation for app settings. return ConfigurationSettings.AppSettings; } } } Then for each custom config handler that your app exposes expose another static property which maps to the object which is returned by the Create method of the custom section interface. Clear as mud? The eventual outcome is that you get a one-stop configuration shop which is really quite useful, I think a tool that generates this configuration architectures would be really nifty. Doesn't really address the write issue though. Take a look at the <configSections /> element in the .NET configuration file (documentation address follows). If you look in the Machine.config file you can see these used all over the place, but you can also use them in your Web.config file and other application specific configuration files. ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/gngrfconfigsectionselementcont ainertag.htm Hope this helps. ---------------------------------------- - Mitch Denny - [EMAIL PROTECTED] - +61 (414) 610-141 - -----Original Message----- From: The DOTNET list will be retired 7/1/02 [mailto:[EMAIL PROTECTED]] On Behalf Of dave wanta Sent: Sunday, 16 June 2002 22:49 To: [EMAIL PROTECTED] Subject: Re: [DOTNET] web.config Ok, Thanks! Just wondering if I was missing something. Rigth now I'm just doing some research before I start coding. My next question is, wouldn't it be easier to create an object that represents the web.config and then just use the XMLSerializer to read and write it to and from the disk? My XML is a little weak, so I haven't fully explored this option. Questions? Comments? Smart Remarks <g>? Thanks, Dave ----- Original Message ----- From: "Mitch Denny" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, June 16, 2002 5:50 AM Subject: Re: [DOTNET] web.config > Dave, > > Its basically just an XML file. Unfortunately the configuration > architecture in .NET doesn't support writing to the configuration file > with the same ease as reading. Basically you need to open it with an > XmlDocument then work with the DOM. Here is a minmal example of > changing an application setting. > > using ConfWrite; > using System; > using System.Configuration; > using System.Xml; > > namespace ConfWrite > { > > public class EntryPoint > { > > public static void Main() > { > > // Declare locals. > XmlDocument configFile = null; > XmlElement configuration = null; > XmlElement mySetting = null; > > // Load up the application config file. > configFile = new XmlDocument(); > configFile.Load("ConfWrite.exe.config"); > > // Grab the <configuration /> element, well, > atleast > // it should be if it a valid .NET config file. > configuration = configFile.DocumentElement; > > // Grab a particular appSetting with the key of > // appSetting - pretty straight-forward. > mySetting = > (XmlElement)configuration.SelectSingleNode("appSettings/add[@key='MySe > tt > ing']"); > > // Print out the before value. > Console.WriteLine( > "Before: {0}", > mySetting.Attributes["value"].Value > ); > > // Update the value with the current date. > mySetting.Attributes["value"].Value = > DateTime.Now.ToString(); > > // Print out the after value. > Console.WriteLine( > "Before: {0}", > mySetting.Attributes["value"].Value > ); > > // Save the current configuration back to disk. > configFile.Save("ConfWrite.exe.config"); > > } > > } > > } > > When you implement IConfigurationSectionHandler you get passed an > XmlNode instance, but if you grab the document reference from that and > try and persist it to disk you end up overriting the config file with > just that element > -- disaster for anyone who attempts it and doesn't back-up first. The > other thing you may run into is a file locking issue where the > configuration file gets locked during the process of a read so you > need to space writes out from the reads (if using the default > mechanism). > > There is probably a market for a two way configuration reading library > which can support IConfigurationSectionHandler interface but also adds > two more: > > IConfigurationSectionWriter > IConfigurationSectionReader > > Maybe Microsoft might implement something like this in > future versions of the .NET Framework. > > ---------------------------------------- > - Mitch Denny > - [EMAIL PROTECTED] > - +61 (414) 610-141 > - > > -----Original Message----- > From: The DOTNET list will be retired 7/1/02 > [mailto:[EMAIL PROTECTED]] On Behalf Of dave wanta > Sent: Sunday, 16 June 2002 08:59 > To: [EMAIL PROTECTED] > Subject: [DOTNET] web.config > > > hi all, > Does anyone know of any urls\examples for programmatically modifying > the web.config? > > Thanks, > Dave > > You can read messages from the DOTNET archive, unsubscribe from > DOTNET, or subscribe to other DevelopMentor lists at > http://discuss.develop.com. > > You can read messages from the DOTNET archive, unsubscribe from > DOTNET, or subscribe to other DevelopMentor lists at > http://discuss.develop.com. > You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.