从C#.net中的表单字段更新xml文件
您好,我有以下类提供对xml文件的访问:从C#.net中的表单字段更新xml文件,c#,.net,xml,C#,.net,Xml,您好,我有以下类提供对xml文件的访问: public class AppConfig { public string AppConfigPath = "~/admin.config.xml"; XmlDocument SiteConfig = new XmlDocument(); public string getAppConfigParam(string param) { SiteConfig.Load(HttpContext.Curre
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);
}
}
我在下面的事件中使用该类来更新一些应用程序设置,但只有第一个节点(globalskin)得到更新
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());
}
}
要更改所有字段,我需要进行哪些更改?
感谢您抽出时间。我怀疑您的
setAppConfigParam
方法中的这一行:
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl);
正在导致立即重定向,因此对
setAppConfigParam
的其他两个调用甚至从未执行过。此xml文件是您的web配置文件(或“关键文件”)吗?如果是这样,那么当您保存配置文件时,它将重新启动appdomain。是否有理由将配置文件视为普通的旧xml文档?如果它实际上是一个.net配置文件,…文档说明“必须先调用Initialize方法才能使用此方法。”…您这样做了吗?此文件不是.net应用程序配置文件。它只是一个普通的xml文件,保存有关用户皮肤、新闻提要路径等的信息