Brief description of the problem:
In web development, configuring web.cofig is a task that non-technical personnel cannot do. However, when customers often need to perform simple configuration themselves, an effective tool needs to be provided to guide customers to complete this operation. and prevent invalid or incorrect changes.
Solution:
First of all, you must understand that the system configuration mainly includes two parts: machine.config and web.config. These two files are essentially Xml files and 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 use the read and write operations on the Xml file to achieve the idea of rapid configuration. Here we mainly use web.config as an example to illustrate. The content represented by each data item in Web.config is not the focus of the discussion. For specific content, please refer to the description of Msdn.
The core code implemented is:
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=";";
//Manipulate XML nodes
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("myXML.xml");
XmlNode xNode=xmlDoc.SelectSingleNode("//appSettings/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 up successfully!");
}
In the code, we take myXML.xml as an example, which can represent any other XML modification.
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 various information.
Source: "Anytao"