We will target the server node for this example. We will setup our page to include a few servers in a drop-down like the previous section demonstrated. We will add values of 'Server01', 'Server02', and 'Server03'. After adding these, our code should look like this:
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:
9: myConfig.Load("C:\InetPub\wwwroot\web.config")
10:
11: If not isPostBack then
12: Dim myCurrentValue as String
13: Dim myListItem as ListItem
14:
15: myCurrentValue = myConfig.SelectSingleNode(
"configuration/appSettings/add[@key='conServer']
/@value").Value
16: myListItem = node01.Items.FindByText(myCurrentValue)
17:
18: node01.SelectedIndex = node01.Items.IndexOf(myListItem)
19: Else
20: 'We will save this for later!
21: End If
22:
23: End Sub
24:
25: </script>
26: <html>
27: <head>
28: <title>Web.config Editor</title>
29: </head>
30: <body>
31: <form runat="server">
32: <asp:Label id="title01" runat="server" text="Change Server:" />
33: <asp:DropDownList id="node01" runat="server">
34: <asp:ListItem Value="localhost" text="localhost" />
35: <asp:ListItem Value="Server01" text="Server01" />
36: <asp:ListItem Value="Server02" text="Server02" />
37: <asp:ListItem Value="Server03" text="Server03" />
38: </asp:DropDownList>
39: <br />
40: </form>
41: </body>
42: </html>
Notice how the SelectSingleNode option changed. We are now using a parameter to select which 'add' node we want to use. Think of this as a SELECT SQL statement. We are selecting the node where 'key' is equal to 'conServer'. The attribute of the node is assigned by using the ampersand(@) symbol. Then, the value you want to select is placed inside single quotes('). Finally, the entire statement is placed in between brackets([ and ]).