Seeking Suggestions for Reading Web.Config

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 thoughts on “Seeking Suggestions for Reading Web.Config

  1. 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.

  2. 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();

Leave a Reply