0

Hi I have the following class which provides access to a xml file:

public class AppConfig
{
    public string AppConfigPath = "~/admin.config.xml";
    XmlDocument SiteConfig = new XmlDocument();

    public string getAppConfigParam(string param) 
    {

        SiteConfig.Load(HttpContext.Current.Server.MapPath(AppConfigPath));
        string reqParam = SiteConfig.SelectSingleNode("//cmsAppConfig")[param].InnerText;
        return reqParam;
    }

    public void setAppConfigParam(string paramTitle, string paramValue) 
    {
        SiteConfig.Load(HttpContext.Current.Server.MapPath(AppConfigPath));

        XmlNodeList ConfigNodes = SiteConfig.SelectSingleNode("//cmsAppConfig").ChildNodes;

        foreach (XmlNode node in ConfigNodes) 
        {
            if (node.Name == paramTitle) 
            {
                node.InnerText = paramValue;
            }
        }

        SiteConfig.Save(HttpContext.Current.Server.MapPath(AppConfigPath));
        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl);
    }

}

I am using the class in the following event to update some app settings but only the first node which is the globalskin gets updated.

protected void btnSaveAppConfig_Click(object sender, EventArgs e)
{

    if (IsValid) {
        AppConfig myAppConfig = new AppConfig();
        myAppConfig.setAppConfigParam("globalskin", drpAppTheme.SelectedValue.ToString());
        myAppConfig.setAppConfigParam("homefeedsurl", txtNewsFeedUrl.Text.Trim());
        myAppConfig.setAppConfigParam("homefeedstitle", txtNewsFeedTitle.Text.Trim());
    }
}

What changes to do I need to make to make the change to all fields? Thank you for your time.

makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76
  • The docs state "The Initialize method must be called before this method can be used."...have you done that? http://msdn.microsoft.com/en-us/library/ee784825(v=CS.20).aspx – Aaron McIver Dec 09 '10 at 20:50

2 Answers2

1

I suspect that this line in your setAppConfigParam method:

HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl);

Is causing an immediate redirect, so that the other two calls to setAppConfigParam never even get executed.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

Is this xml file your web config (or 'critical file')? If so, then when you save the config file, it's restarting your appdomain. Is there a reason you're treating the config file as a plain old xml doc? If it is, in fact, a .net configuration file, try this method...

Community
  • 1
  • 1
Cj S.
  • 1,173
  • 8
  • 13
  • This file is not the .net app config file. it's just a normal xml file which holds information about user skin, news feeds path etc – makeitmorehuman Dec 09 '10 at 21:02