-----------------------------web.config----------------------------
<appSettings>
<add key="url" value="1" />
</appSettings>
---------------------------xmlfile.xml-------------------------------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>1</appSettings>
</configuration>
------------------------------------------------------------------------
public static void Modify(string key,string strValue)
{
string XPath="/configuration/appSettings/add[@key='?']";
XmlDocument domWebConfig=new XmlDocument();
domWebConfig.Load( (HttpContext.Current.Server.MapPath("web.config")) );
XmlNode addKey=domWebConfig.SelectSingleNode( (XPath.Replace("?",key)) );
if(addKey == null)
{
throw new ArgumentException("没有找到<add key='"+key+"' value=/>的配置节");
}
addKey.Attributes["value"].InnerText=strValue;
domWebConfig.Save( (HttpContext.Current.Server.MapPath("web.config")) );
}
public static void ModifyXml(string key, string strValue)
{
string XPath = "/configuration/appSettings";
XmlDocument domWebConfig = new XmlDocument();
domWebConfig.Load((HttpContext.Current.Server.MapPath("XMLFile.xml")));
XmlNode addKey = domWebConfig.SelectSingleNode((XPath));
if (addKey == null)
{
throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
}
XmlElement xe = (XmlElement)addKey;
xe.InnerText = strValue;
domWebConfig.Save((HttpContext.Current.Server.MapPath("XMLFile.xml")));
}