Development Notes: Quick configuration of Web. config
Development Notes: Quick configuration of Web. config
Author: anytao
Problem description:
In web development. cofig does not work as a technician, but it is often necessary for the customer to perform simple configuration on their own. It is necessary to provide an effective tool to guide the customer to complete this operation, and prevent invalid or incorrect changes.
Solution:
First, you must understand that the System Configuration mainly includes machine. config and web. config. These two files are essentially XML files that contain all the configuration information of ASP. NET. Therefore, the configuration of the system is actually an operation on the XML file. Therefore, we can adopt the read and write operations on the XML file to achieve quick configuration. Here, we take Web. config as an example to illustrate that the content of each data item in Web. config is not the focus of the discussion. For details, refer to the description of msdn.
Core of implementationCodeIs:
Private void btnok_click (Object sender, system. eventargs E)
{
// Define variables
String strlocation = txtlocation. text;
String strprovider = txtprovider. text;
String strmode = txtmode. text;
String struser = txtuser. text;
String strdatasource = txtdatasource. text;
String strpwd = txtpwd. text;
String semicolon = ";";
// Operate XML nodes
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("myxml. xml ");
Xmlnode xnode = xmldoc. selectsinglenode ("// deleteapps/ add [@ key = 'oledbconnection1. connectionstring ']");
If (xnode! = NULL)
{
Xnode. attributes ["value"]. value = "location =" + strlocation + semicolon + "provider =" + strprovider + semicolon +
"Mode =" + strmode + semicolon + "User ID =" + struser + semicolon + "Data Source =" + strdatasource + semicolon +
"Password =" + strpwd;
}
Xmldoc. Save ("myxml. xml ");
MessageBox. Show ("set successfully! ");
}
In the code, we use myxml. XML as an example to represent any other XML modifications.
These are just simple operations on a data item, and further operations need to be improved.
On the following operation interface, non-technical personnel can easily modify the information.
I hope you can give me more comments. If you need to discuss the code or leave it in your mailbox, I hope you will have a smile.
Enjoy.