0
コンソールアプリケーションのapp.configで接続文字列のユーザーIDとパスワードを更新しようとしています。私が取得:型のオブジェクトをキャストすることができませんタイプ 'System.Xml.XmlComment'のオブジェクトを 'System.Xml.XmlElement'にキャストできません
'をSystem.Xml.XmlComment' 以下のコードで 'System.Xml.XmlElement'
を入力します。太字の言葉は、どこでエラーが発生するかを示します。代わりに、XMLはのConfigurationManagerを使用して編集してみ使用してそれを行うの
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)
。 http://stackoverflow.com/questions/19429477/how-can-i-change-my-app-config-files-connection-string-at-runtime – FloatingKiwi
私はちょうどユーザーIDとパスワードを変更したい@@ – belltric
これらのxmlフィールドではないので、文字列の置換を行う必要があります。 Regex.Replaceを使ってこのことをお勧めします。既存の接続文字列をつかみ、それを変換してファイルに書き戻します。 – FloatingKiwi