The Web. config file assumes the following configuration information needs to be managed:
<Deleetask>
<Add key = "sitetitle" value = "site name"/>
<Add key = "siteurl" value = "home site"/>
<Add key = "sitelogo" value = "site logo"/>
<Add key = "sitebanner" value = "site Banner"/>
<Add key = "siteemail" value = "contact email"/>
</Appsettings>
Implemented C # CoreCode:
1. Read related information in Web. config into textbox
Private Void Page_load ( Object Sender, system. eventargs E)
{
If ( ! Page. ispostback)
{
// Enter the related values in Web. config in textbox.
This . Txttitle. Text = System. configuration. configurationsettings. deleettings [ " Sitetitle " ];
This . Txturl. Text = System. configuration. configurationsettings. deleettings [ " Siteurl " ];
This . Txtlogo. Text = System. configuration. configurationsettings. deleettings [ " Sitelogo " ];
This . Txtbanner. Text = System. configuration. configurationsettings. deleettings [ " Sitebanner " ];
This . Txtemail. Text = System. configuration. configurationsettings. deleettings [ " Siteemail " ];
}
}
2. Write the modified content to Web. config
Private Void Btnsave_click ( Object Sender, system. eventargs E)
{
String Filename = Server. mappath ( " Web. config " );
String Keyname; // Key name
Xmldocument xmldoc = New Xmldocument ();
Try
{
Xmldoc. Load (filename );
}
Catch
{
Response. Write ("<SCRIPT> alert ('file reading error. Check whether the Web. config file exists! ') </SCRIPT>");
Return;
}
Xmlnodelist docdnodenamearr = Xmldoc. documentelement. childnodes; // Document node name Array
Foreach (Xmlelement docxmlelement In Docdnodenamearr)
{
If (Docxmlelement. Name. tolower () = " Appsettings " ) // Find the node named "deleettings"
{
Xmlnodelist keynamearr = Docxmlelement. childnodes; // Array of subnode names
If (Keynamearr. Count > 0 )
{
Foreach (Xmlelement In Keynamearr)
{
Keyname = Xmlelement. attributes [ " Key " ]. Innerxml; // Key Value
Switch (Keyname)
{
Case " Sitetitle " :
Xmlelement. attributes [ " Value " ]. Value = This . Txttitle. text;
Break ;
Case " Siteurl " :
Xmlelement. attributes [ " Value " ]. Value = This . Txturl. text;
Break ;
Case " Sitelogo " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtlogo. text;
Break ;
Case " Sitebanner " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtbanner. text;
Break ;
Case " Siteemail " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtemail. text;
Break ;
}
}
}
}
}
Try
{
Xmldoc. Save (filename );
Response. Write ("<SCRIPT> alert ('OK, the information has been saved! ') </SCRIPT>");
}
Catch
{
Response. Write ("<SCRIPT> alert ('file writing error. Check whether the Web. config file exists! ') </SCRIPT>");
Return;
}
}