Unable to cast object of type 'System.Xml.XmlComment' to type 'System.Xml.XmlElement'
<p>I'm trying to update connection string user id and password in the app.config of a console application. I get:</p>

<blockquote>
  <p>Unable to cast object of type 'System.Xml.XmlComment' to type
  'System.Xml.XmlElement'</p>
</blockquote>

<p>with the below code. Words in bold indicate where it throw errors.</p>

<pre><code>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)
</code></pre>