dynamic management of Web.config files using programs

Source: Internet
Author: User
Tags foreach array config key return string
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
}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.