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?
Follow me on Twitter
It looks like you could use System.Web.Configuration.WebConfigurationManager too, though it doesn’t look like the code would be any simpler.
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.
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();