开发者

Updating a xml file from form fields in C# .net

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.


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜