您的位置:首页 > 其它

Linq修改Web.config的连接字符串

2016-07-17 20:13 441 查看
1、Linq修改Web.config的连接字符串
string xmlPath = Environment.CurrentDirectory + "\\ModifyConfig.exe.config";
XElement xml = XElement.Load(xmlPath);
var temp = from k in xml.Descendants("connectionStrings").Elements("add")
where k.Attribute("name").Value == "connStr"
select k;
var query = temp.SingleOrDefault();
if (query != null)
{
query.Attribute("connectionString").Value = this.txtValue.Text;
xml.Save(xmlPath);

}

2、Linq修改Web.config的连接字符串

 

var tempapp = from k in xml.Descendants("appSettings").Elements("add")
where k.Attribute("key").Value == "pas"
select k;
var queryapp = temp.SingleOrDefault();

if (query != null)
{
query.Attribute("value").Value = newpassword;
xml.Save(path);

}

3、其他方法

XmlDocument objXML = new XmlDocument();
string xmlPath = Environment.CurrentDirectory + "\\ModifyConfig.exe.config";
objXML.Load(xmlPath);
XmlNode rootNode = objXML.DocumentElement;
foreach (XmlNode objNode in rootNode.ChildNodes)
{
if (objNode.Name == "connectionStrings")
{
objNode.FirstChild.Attributes["connectionString"].Value = this.txtValue.Text.Trim();

objXML.Save(xmlPath);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: