Read settings from Web.config file (or other XML files)

Originally from

'****************************** GetConfigValue *******************************
' Purpose: Utility function to get value from a configuration file.
' Conditions: CONFIG_FILE_PATH must be refer to a valid XML file
' Input: sectionName - a section in the file, eg, appSettings
' attrName - refers to the "key" attribute of an entry
' Output: A string containing the value of the appropriate entry

CONFIG_FILE_PATH = "Web.config" 'if no qualifier, refers to this directory. can point elsewhere.

Function GetConfigValue(sectionName, attrName)
Dim oXML, oNode, oChild, oAttr, dsn
Set oXML=Server.CreateObject("Microsoft.XMLDOM")
oXML.Async = "false"
Set oNode = oXML.GetElementsByTagName(sectionName).Item(0)
Set oChild = oNode.GetElementsByTagName("add")
' Get the first match
For Each oAttr in oChild
If oAttr.getAttribute("key") = attrName then
dsn = oAttr.getAttribute("value")
GetConfigValue = dsn
Exit Function
End If
End Function


ectionName, attrName


settingValue = GetConfigValue(&#34;appSettings&#34;, &#34;someKeyName&#34;)<br/>Response.Write(settingValue)

Views 76 Downloads 35

Perm link