web| Program | dynamic
The Web.config file assumes the following configuration information to be managed:
<appSettings>
<add key= "Sitetitle" value= "site name"/>
<add key= "SiteURL" value= "Homepage url"/>
<add key= "Sitelogo" value= "site logo"/>
<add key= "Sitebanner" value= "site Banner"/>
<add key= "Siteemail" value= "contact email"/>
</appSettings>
Implementation of the C # core code:
First, read the relevant information in the Web.config into the textbox
private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
Fill in textbox with related values in Web.config
this.txttitle.text=system.configuration.configurationsettings.appsettings["Sitetitle"];
this.txturl.text=system.configuration.configurationsettings.appsettings["SiteURL"];
this.txtlogo.text=system.configuration.configurationsettings.appsettings["Sitelogo"];
this.txtbanner.text=system.configuration.configurationsettings.appsettings["SiteBanner"];
this.txtemail.text=system.configuration.configurationsettings.appsettings["Siteemail"];
}
}
Ii. 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 (Error when reading file, check 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 a node named appsettings
{
XmlNodeList keynamearr=docxmlelement.childnodes;//Array of child node names
if (Keynamearr.count >0)
{
foreach (XmlElement 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, information saved! ') </script> ");
}
Catch
{
Response.Write (' <script>alert error while writing file, check Web.config file exists! ') </script> ");
Return
}
}