COMING SOON: Web.Config Editor Application
For more information, go here.
Now that we know how to edit basic properties in the web.config, let's create some custom
properties. For this example, I have added two custom nodes as shown below:
<configuration> <appSettings> <add key="conUser" Value="sa"> <add key="conServer" value="localhost"> </appSettings> </configuration>
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:
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 ]).
Then, we will use the same concept as Part 1 to save the node. This time we will
use the a parameter to select which 'add' node we want to update.
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: Sub Button_Click(s as Object, e as EventArgs) 26: Dim myConfig As New XmlDocument() 27: Dim myAttribute As XmlAttribute 28: 29: myConfig.Load("C:\InetPub\wwwroot\web.config") 30: 31: myAttribute = myConfig.SelectSingleNode( "configuration/appSettings/add[@key='conServer']/@value") 32: myAttribute.Value = node01.SelectedItem.ToString() 33: 34: myConfig.Save("C:\InetPub\wwwroot\web.config") 35: End Sub 36: 37: </script> 38: <html> 39: <head> 40: <title>Web.config Editor</title> 41: </head> 42: <body> 43: <form runat="server"> 44: <asp:Label id="title01" runat="server" text="Change Server:" /> 45: <asp:DropDownList id="node01" runat="server"> 46: <asp:ListItem Value="localhost" text="localhost" /> 47: <asp:ListItem Value="Server01" text="Server01" /> 48: <asp:ListItem Value="Server02" text="Server02" /> 49: <asp:ListItem Value="Server03" text="Server03" /> 50: </asp:DropDownList> 51: <br /> 52: <asp:Button id="myButton" runat="server" Text="Update" OnClick="Button_Click" /> 53: </form> 54: </body> 55: </html>
For more information on this topic, feel free to contact Jason at the e-mail address above.
Related Resources
Simple XSLT&XPath grouping (Teemu Keiski)
[My Column]
[More XML Articles]
[Part 1]
[Printer-Friendly]
|