2016-08-17 18 views
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) 
+0

。 http://stackoverflow.com/questions/19429477/how-can-i-change-my-app-config-files-connection-string-at-runtime – FloatingKiwi

+0

私はちょうどユーザーIDとパスワードを変更したい@@ – belltric

+0

これらのxmlフィールドではないので、文字列の置換を行う必要があります。 Regex.Replaceを使ってこのことをお勧めします。既存の接続文字列をつかみ、それを変換してファイルに書き戻します。 – FloatingKiwi

答えて

-2
foreach (XmlLinkedNode xe in xn.ChildNodes){ 
    if (xe.NodeType != XmlNodeType.Comment){ 

    } 
} 
関連する問題