Creating a Web.config Editor - Part 1
page 4 of 5
by Jason N. Gaylord
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25989/ 147

Section 2: Prepare Our Page
We must first prepare our document for this type of editing like shown below:
EditConfig.aspx
**************************************************

1:   <%@ Page Language="vb" %>
2:   <%@ import Namespace="System" %>
3:   <%@ import Namespace="System.Xml" %>
4:   <script runat="server">
5:   
6:       Sub Page_Load(s as Object, e as EventArgs)
7:          Dim myConfig As New XmlDocument()
8:          Dim myAttColl As XmlAttributeCollection
9:          Dim myAttribute As XmlAttribute
10:    
11:         myConfig.Load("C:\InetPub\wwwroot\web.config")
12:           
13:         If not isPostBack then
14:              Dim myCurrentValue as String
15:              Dim myListItem as ListItem
16:          
17:              myCurrentValue = myConfig.SelectSingleNode(
                     "configuration/system.web/customErrors/@mode").Value
18:              myListItem = node01.Items.FindByText(myCurrentValue)
19:    
20:              node01.SelectedIndex = node01.Items.IndexOf(myListItem)
21:           Else
22:              'We will save this for later!
23:           End If
24:        
25:        End Sub
26:    
27:    </script>
28:    <html>
29:    <head>
30:        <title>Web.config Editor</title>
31:    </head>
32:    <body>
33:    
34:    <form runat="server">
35:        <asp:Label id="title01" runat="server" text="Custom Errors:" />
36:        <asp:DropDownList id="node01" runat="server">
37:          <asp:ListItem Value="On" text="On" />
38:          <asp:ListItem Value="Off" text="Off" />
39:          <asp:ListItem Value="RemoteOnly" text="RemoteOnly" />
40:        </asp:DropDownList><br />
41:    </form>
42:    
43:    </body>
44:    </html>

Before we continue, let me explain each critical line. Lines 7-9 set up our XML variables. These "guys" are the ones that will conatin the xml properties. For this example, we won't use the XmlAttributeCollection (line 8). Line 11 loads the XML file into memory. The XML file in this case is the web.config file. Line 17 is where the actual node is loaded into the application. The SelectSingleNode method selects only a specific node in the web.config file. In this instance, the method selects the root ("configuration/system.web/") and then selects the node and attribute ("customErrors/@mode"). Finally, because this value is returned as an XmlAttribute type, we must get the value to convert it into a string value. Line 18 searches for the index value of the particular text in the drop-down control below. In the default web.config, the index will be returned as 2. Line 20 then sets the selected value of the drop-down to be equal to the index value returned in line 18. The drop-down list from lines 36-40 show the possible values for the customErrors node in the web.config file.

View Entire Article

User Comments

Title: I Have question   
Name: OMAR ALZOBAIDI
Date: 2006-05-08 2:25:38 AM
Comment:
How to use Location Tag in web.config file?
-------------------------------------------
and .......... Thanks !!!
Title: Excellent trial for NewToXml   
Name: Baljeet Kiroriwal
Date: 2006-03-02 7:46:07 AM
Comment:
Good Article n gr8 information
Title: What if the solution is in a source safe   
Name: m7e7
Date: 2004-08-05 4:03:37 PM
Comment:
What if the soln is in a source safe vault?
Title: The problems I had   
Name: Sergio Florez
Date: 2004-07-23 9:51:21 AM
Comment:
First, sorry if I mix up the first and second part of the article but here goes:

You're capturing the return of SelectSingleNode() in an XmlAttribute and that didn't work for me. It said it needed a NodeSet. I fixed this by capturing it with an XmlNode and working with its Attributes collection.

The other problem I had was that your XPath didn't capture the node for me so I had to use this:
"descendant::appSettings/add[@key='conServer']"
Title: RE: Thanks but no thanks   
Name: Jason
Date: 2004-07-22 4:35:10 PM
Comment:
I wish you'd let me know what isn't working. This way I can provide some "troubleshooting" for you.
Title: Thanks but no thanks   
Name: Sergio Florez
Date: 2004-07-22 11:31:08 AM
Comment:
This article's basic concept of trying to modify the web.config is great, unfortunately the code doesn't work for me. Maybe you should try and write some troubleshooting for this.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2022 ASPAlliance.com  |  Page Processed at 2022-09-24 3:18:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search