0
我試圖更新控制檯應用程序的app.config中的連接字符串用戶標識和密碼。我得到:無法將類型爲'System.Xml.XmlComment'的對象轉換爲鍵入'System.Xml.XmlElement'
無法投類型的對象System.Xml.XmlComment'與下面的代碼輸入 'System.Xml.XmlElement'
。粗體字表示錯誤的位置。
Dim XmlDoc As New XmlDocument()
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
**For Each xElement As XmlElement In XmlDoc.DocumentElement**
If xElement.Name = "connectionStrings" Then
Dim conStringBuilder As New SqlConnectionStringBuilder(sConnectionString)
conStringBuilder.UserID = sNewUserID
conStringBuilder.Password = sNewPwd
xElement.FirstChild.Attributes(2).Value = conStringBuilder.ToString()
End If
Next
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
而不是使用XML嘗試使用ConfigurationManager中編輯這樣做的。 http://stackoverflow.com/questions/19429477/how-can-i-change-my-app-config-files-connection-string-at-runtime – FloatingKiwi
我只是想改變用戶名和密碼@@ – belltric
這些不是xml字段,所以你需要做一個字符串替換。我建議使用Regex.Replace。因此,抓住現有的連接字符串,進行轉換,然後將其寫回文件。 – FloatingKiwi