Seeking Suggestions for Reading Web.Config

RadicalWacko March 24th, 2008

I recently had a task in one of my current projects that required me to extract the authentication mode of the current solution’s web.config file. The only was I figured out how to do it was by parsing the XML of the file by hand as shown below (Yeah, I know, it’s VB.NET. I do what the client wants.):

 

            Dim config As New System.Xml.XmlDocument()
            config.Load(Server.MapPath(\Web.config))

            Dim authMode As System.Xml.XmlNode = config.SelectSingleNode(/configuration/system.web/authentication/@mode)
            Return authMode.Value

While that works just fine, it seems a little primitive and I figured Microsoft would have introduced a library to get that kind of data. Anyone know of any?

3 Responses to “Seeking Suggestions for Reading Web.Config”

  1. dylanon 25 Mar 2008 at 10:58 am

    It looks like you could use System.Web.Configuration.WebConfigurationManager too, though it doesn’t look like the code would be any simpler.

  2. RadicalWackoon 30 Mar 2008 at 1:14 am

    Yeah, I took a look and it doesn’t seem to be any easier. Of course, they are probably a bit safer about thread safety than I am, but I only read it once, so no need to change it now. Thanks for the pointer.

  3. Peteon 17 Apr 2008 at 7:55 pm

    This probably won’t help at all….but I couldn’t resist. Here’s my version using LINQ to XML in a single line of code:

    return (from nd in XElement.Load(Server.MapPath(”web.config”)).Descendants(”authentication”)
    select nd.Attribute(”mode”).Value).Single();

Trackback URI | Comments RSS

Leave a Reply

Rodney's Widget for the FAlbum. plugged in.