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