如何用代码修改web.config中的配置
我的web.config内容如下:<configuration><connectionStrings><addname="acountConnectionString"c...
我的web.config内容如下:
<configuration>
<connectionStrings>
<add name="acountConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\acount.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
<add name="mailaddress" connectionString="haosafe@sina.com"/>
</connectionStrings>
<system.net>
<mailSettings>
<smtp>
<network host="smtp.sina.com" port="25" userName="haosafe@sina.com" password="wuhao0512"/>
</smtp>
</mailSettings>
</system.net>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
</configuration>
现在我要把 <connectionStrings>的两个字符串值以及 <network host="smtp.sina.com" port="25" userName="haosafe@sina.com" password="wuhao0512"/>
中的host username和password的值通过代码用字符串s1,s2,h,user和pass来赋值,该怎么改。要源代码。 展开
<configuration>
<connectionStrings>
<add name="acountConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\acount.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
<add name="mailaddress" connectionString="haosafe@sina.com"/>
</connectionStrings>
<system.net>
<mailSettings>
<smtp>
<network host="smtp.sina.com" port="25" userName="haosafe@sina.com" password="wuhao0512"/>
</smtp>
</mailSettings>
</system.net>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
</configuration>
现在我要把 <connectionStrings>的两个字符串值以及 <network host="smtp.sina.com" port="25" userName="haosafe@sina.com" password="wuhao0512"/>
中的host username和password的值通过代码用字符串s1,s2,h,user和pass来赋值,该怎么改。要源代码。 展开
1个回答
展开全部
代码如下:请导入 System.XML命名空间
string file = Server.MapPath(@"~\web.config");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
string s1 = "aaa";
string s2 = "bbb";
string h = "ccc";
string user = "ddd";
string pass = "eee";
XmlNodeList nodeList = null;
nodeList = xmlDoc.SelectSingleNode("configuration//connectionStrings").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = xn as XmlElement;
if (xe.Name == "add")
{
if (xe.GetAttribute("name") == "acountConnectionString")
{
xe.SetAttribute("connectionString", s1);
}
if (xe.GetAttribute("name") == "mailaddress")
{
xe.SetAttribute("connectionString", s2);
}
}
}
nodeList = xmlDoc.SelectSingleNode("configuration//system.net//mailSettings//smtp").ChildNodes;
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = xn as XmlElement;
if (xe.Name == "network")
{
xe.SetAttribute("host", h);
xe.SetAttribute("userName", user);
xe.SetAttribute("password", pass);
break;
}
}
xmlDoc.Save(file);
但实际上通过代码修改web.config的操作微乎其微
1. web.config的修改可能会导致session等服务器变量的丢失
2. 如果你的页面是发布在IIS下面,要通过页面修改web.config,必须给web.config这个文件添加 Network service (IIS6)或 ASPNET (IIS)用户的写权限,这在实际操作中是不可想象的
string file = Server.MapPath(@"~\web.config");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
string s1 = "aaa";
string s2 = "bbb";
string h = "ccc";
string user = "ddd";
string pass = "eee";
XmlNodeList nodeList = null;
nodeList = xmlDoc.SelectSingleNode("configuration//connectionStrings").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = xn as XmlElement;
if (xe.Name == "add")
{
if (xe.GetAttribute("name") == "acountConnectionString")
{
xe.SetAttribute("connectionString", s1);
}
if (xe.GetAttribute("name") == "mailaddress")
{
xe.SetAttribute("connectionString", s2);
}
}
}
nodeList = xmlDoc.SelectSingleNode("configuration//system.net//mailSettings//smtp").ChildNodes;
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = xn as XmlElement;
if (xe.Name == "network")
{
xe.SetAttribute("host", h);
xe.SetAttribute("userName", user);
xe.SetAttribute("password", pass);
break;
}
}
xmlDoc.Save(file);
但实际上通过代码修改web.config的操作微乎其微
1. web.config的修改可能会导致session等服务器变量的丢失
2. 如果你的页面是发布在IIS下面,要通过页面修改web.config,必须给web.config这个文件添加 Network service (IIS6)或 ASPNET (IIS)用户的写权限,这在实际操作中是不可想象的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询