You want to be able to read application
information
from a web.config
file that is not available as
an <appSettings>
key
/value
pair, but present as
an attribute or child element of some other element of the file.
Read the web.config
file into an
XmlDocument
object, and access the target element
that contains the information you need as you would any other XML
document node.
In the code-behind class for your ASP.NET page, use the .NET language of your choice to:
Read the web.config
file into an
XmlDocument
object.
Use the SelectSingleNode
method to get a reference
to the desired section.
Use the attributes collection of the selected node to access the attributes for the section.
Example 9-6 through Example 9-8 show an application we’ve
written that implements this solution and retrieves attribute
settings from the <trace>
element of a
web.config
file. Example 9-6
shows the .aspx
file that displays the
information. Example 9-7 (VB) and Example 9-8 (C#) show the code-behind class for the page
that does the work of reading the settings from the
<trace>
element.
Because the web.config
file is an XML document,
you can process it as you would any other XML document and use the
XML classes provided in the .NET Framework to access its elements and
their children. The first step is to create a new
XmlDocument
object and load the
web.config
file into it. In our example, we use
Server.MapPath
to get the fully qualified name for
the web.config
file:
xmlDoc = New XmlDocument
xmlDoc.Load(Server.MapPath("web.config"))
xmlDoc = new XmlDocument( );
xmlDoc.Load(Server.MapPath("web.config"));
Next, use the SelectSingleNode
method, passing it
the fully qualified path from the root of the XML document to the
desired section of the web.config
file. In our
application, for example, we want to access the
<trace>
section:
xmlNode = xmlDoc.SelectSingleNode("configuration/system.web/trace")
xmlNode = xmlDoc.SelectSingleNode("configuration/system.web/trace");
After getting a reference to the desired section (or
node
in XML terminology), the attributes
collection of the node, which contains all of the attribute
information for the requested web.config
section, is available. In our example, we use the
GetNamedItem
method of the attributes collection
to access the specific attributes whose values we want. If you need
to display all of the attributes from a section, you can iterate
through the attributes collection using the name and value properties
of each attribute to read and then output the information.
The approach described in this recipe can provide access to the
attributes of any element in the web.config
file. And, as a bonus, you can use the same method to
programmatically change any of those attributes. The following code,
for example, changes the value of the enabled
attribute of <trace>
to
true
and then saves the
web.config
file with the new value. When the
file is saved, ASP.NET, which monitors the
web.config
file for changes, restarts the
application.
attributes.GetNamedItem("enabled").Value = "true" xmlDoc.Save(Server.MapPath("web.config")) attributes.GetNamedItem("enabled").Value = "true"; xmlDoc.Save(Server.MapPath("web.config"));
Example 9-6. Access system configuration information in web.config (.aspx)
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="CH09AccessSystemConfigVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH09AccessSystemConfigVB"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Accessing System Configuration Information</title> <link rel="stylesheet" href="css/ASPNetCookbook.css"> </head> <body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0"> <form id="frmConfiguration" method="post" runat="server"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td align="center"> <img src="images/ASPNETCookbookHeading_blue.gif"> </td> </tr> <tr> <td class="dividerLine"> <img src="images/spacer.gif" height="6" border="0"></td> </tr> </table> <table width="90%" align="center" border="0"> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center" class="PageHeading"> Accessing System Configuration Information In web.config (VB) </td> </tr> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center"> <table width="40%"> <tr> <td align="center" class="SubHeading" colspan="2"> trace Section Attributes </td> </tr> <tr> <td align="right" width="50%" class="LabelText">enabled = </td> <td width="50%" class="LabelText"> <asp:Label ID="labEnabled" runat="server" /> </td> </tr> <tr> <td align="right" class="LabelText">requestLimit = </td> <td class="LabelText"> <asp:Label ID="labRequestLimit" runat="server" /> </td> </tr> <tr> <td align="right" class="LabelText">pageOutput = </td> <td class="LabelText"> <asp:Label ID="labPageOutput" runat="server" /> </td> </tr> <tr> <td align="right" class="LabelText">traceMode = </td> <td class="LabelText"> <asp:Label ID="labTraceMode" runat="server" /> </td> </tr> <tr> <td align="right" class="LabelText">localOnly = </td> <td class="LabelText"> <asp:Label ID="labLocalOnly" runat="server" /> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>
Example 9-7. Access system configuration information in web.config code-behind (.vb)
Option Explicit On Option Strict On '----------------------------------------------------------------------------- ' ' Module Name: CH09AccessSystemConfigVB.aspx.vb ' ' Description: This module provides the code behind for the ' CH09AccessSystemConfigVB.aspx page ' '***************************************************************************** Imports Microsoft.VisualBasic Imports System Imports System.Xml Namespace ASPNetCookbook.VBExamples Public Class CH09AccessSystemConfigVB Inherits System.Web.UI.Page 'controls on the form Protected labEnabled As System.Web.UI.WebControls.Label Protected labRequestLimit As System.Web.UI.WebControls.Label Protected labPageOutput As System.Web.UI.WebControls.Label Protected labTraceMode As System.Web.UI.WebControls.Label Protected labLocalOnly As System.Web.UI.WebControls.Label '************************************************************************* ' ' ROUTINE: Page_Load ' ' DESCRIPTION: This routine provides the event handler for the page load ' event. It is responsible for initializing the controls ' on the page. '------------------------------------------------------------------------- Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.LoadDim xmlDoc As XmlDocument
Dim xmlNode As XmlNode
Dim attributes As XmlAttributeCollection
'load web.config
xmlDoc = New XmlDocument
xmlDoc.Load(Server.MapPath("web.config"))
'get the trace section
xmlNode = xmlDoc.SelectSingleNode("configuration/system.web/trace")
If (Not IsNothing(xmlNode)) Then
'the trace section is a collection or attributes so get the
'collection then output the individual values
attributes = xmlNode.Attributes
labEnabled.Text = attributes.GetNamedItem("enabled").Value
labRequestLimit.Text = attributes.GetNamedItem("requestLimit").Value
labPageOutput.Text = attributes.GetNamedItem("pageOutput").Value
labTraceMode.Text = attributes.GetNamedItem("traceMode").Value
labLocalOnly.Text = attributes.GetNamedItem("localOnly").Value
End If
End Sub 'Page_Load End Class 'CH09AccessSystemConfigVB End Namespace
Example 9-8. Access system configuration information in web.config code-behind (.cs)
//---------------------------------------------------------------------------- // // Module Name: CH09AccessSystemConfigCS.aspx.cs // // Description: This module provides the code behind for the // CH09AccessSystemConfigCS.aspx page // //**************************************************************************** using System; using System.Xml; namespace ASPNetCookbook.CSExamples { public class CH09AccessSystemConfigCS : System.Web.UI.Page { // controls on the form protected System.Web.UI.WebControls.Label labEnabled; protected System.Web.UI.WebControls.Label labRequestLimit; protected System.Web.UI.WebControls.Label labPageOutput; protected System.Web.UI.WebControls.Label labTraceMode; protected System.Web.UI.WebControls.Label labLocalOnly; //************************************************************************ // // ROUTINE: Page_Load // // DESCRIPTION: This routine provides the event handler for the page // load event. It is responsible for initializing the // controls on the page. //------------------------------------------------------------------------ private void Page_Load(object sender, System.EventArgs e) {XmlDocument xmlDoc = null;
XmlNode xmlNode = null;
XmlAttributeCollection attributes = null;
// load web.config
xmlDoc = new XmlDocument( );
xmlDoc.Load(Server.MapPath("web.config"));
// get the trace section
xmlNode =
xmlDoc.SelectSingleNode("configuration/system.web/trace");
if (xmlNode != null)
{
// the trace section is a collection of attributes so get the
attributes = xmlNode.Attributes;
labEnabled.Text = attributes.GetNamedItem("enabled").Value;
labRequestLimit.Text = attributes.GetNamedItem("requestLimit").Value;
labPageOutput.Text = attributes.GetNamedItem("pageOutput").Value;
labTraceMode.Text = attributes.GetNamedItem("traceMode").Value;
labLocalOnly.Text = attributes.GetNamedItem("localOnly").Value;
}
} // Page_Load } // CH09AccessSystemConfigCS }