Linq To Xml改变App.config中的ConnectionString的Value
2008-04-05 23:21
417 查看
<connectionStrings>
<addname="AppConfigChange.My.MySettings.Connstr"
connectionString=
"Data Source=wghosh2k3"sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
Dim sNewConnStr As String = ""
'Get the file info
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
'Load the file info
Dim xml = XElement.Load(config.FilePath)
'Get the first config section (first connection string info)
Dim connStrXML = xml.Descendants("connectionStrings").Elements().First()
'Get the connection string value
Dim connStr = connStrXML.Attribute("connectionString").Value
'Create an array with ';'
Dim arrConn() As String = connStr.Split(";")
For i As Int16 = 0 To arrConn.Length - 1
'Get the attribute and value splitted by "="
Dim arrSubConn() As String = arrConn(i).Split("=")
If (arrSubConn.Length = 2) Then
Dim sConnAttr As String = ""
Dim sConnValue As String = ""
sConnAttr = arrSubConn(0)
sConnValue = arrSubConn(1)
'Change Database name
If (sConnAttr = "Initial Catalog") Then
'This is the place where you will be changing the database name
sConnValue = "NewDBName"
End If
'Generate newly altered connection string
sNewConnStr += sConnAttr + "=" + sConnValue + ";"
End If
Next
After doing everything you need to save it back to the same file,
'Modify the existing connection string information
connStrXML.SetAttributeValue("connectionString", sNewConnStr)
'Saving config at the same place
xml.Save(config.FilePath)
<addname="AppConfigChange.My.MySettings.Connstr"
connectionString=
"Data Source=wghosh2k3"sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
Dim sNewConnStr As String = ""
'Get the file info
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
'Load the file info
Dim xml = XElement.Load(config.FilePath)
'Get the first config section (first connection string info)
Dim connStrXML = xml.Descendants("connectionStrings").Elements().First()
'Get the connection string value
Dim connStr = connStrXML.Attribute("connectionString").Value
'Create an array with ';'
Dim arrConn() As String = connStr.Split(";")
For i As Int16 = 0 To arrConn.Length - 1
'Get the attribute and value splitted by "="
Dim arrSubConn() As String = arrConn(i).Split("=")
If (arrSubConn.Length = 2) Then
Dim sConnAttr As String = ""
Dim sConnValue As String = ""
sConnAttr = arrSubConn(0)
sConnValue = arrSubConn(1)
'Change Database name
If (sConnAttr = "Initial Catalog") Then
'This is the place where you will be changing the database name
sConnValue = "NewDBName"
End If
'Generate newly altered connection string
sNewConnStr += sConnAttr + "=" + sConnValue + ";"
End If
Next
After doing everything you need to save it back to the same file,
'Modify the existing connection string information
connStrXML.SetAttributeValue("connectionString", sNewConnStr)
'Saving config at the same place
xml.Save(config.FilePath)
相关文章推荐
- 使用linq to xml修改app.config示例(linq读取xml)
- 使用Linq to XML 修改app.config
- LINQ TO XML 修改 app.config
- LINQ To XML:设置子元素的值、添加子元素或移除子元素(XElement.SetElementValue)
- Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your a
- Parsing Failure in config.xml: java.lang.IllegalArgumentException: In production mode, it's not allowed to set a clear text value to the property
- Translate this app.config xml to code? (WCF) z
- LINQ之路19:LINQ to XML之X-DOM更新、和Value属性交互
- LINQ之路19:LINQ to XML之X-DOM更新、和Value属性交互
- C#改变LInqToSQL的引用地址,读取config的数据库字符串
- LINQ to XML 编程基础 创建 增删改查
- Linq to xml 操作带命名空间的xml
- LinQ To XML——用LinQ查询XML
- 【linq学习笔记】1、linq to xml
- Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
- Linq to XML说法——(一)创建,添加,查询
- VS2005(C#)里读取及改变App.config里appSettings节的值
- linq TO XML 基础
- LINQ TO XML
- Linq to XML