You can do the same using the configuration-specific parts of the .NET Framework to save some of the hard work:
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
{
InitialCatalog = "YOUR_DATABASE",
IntegratedSecurity = true,
Password = "YOUR_PASSWORD"
};
config.ConnectionStrings.ConnectionStrings["CONNECTION_NAME"].ConnectionString =
sqlBuilder.ConnectionString;
config.Save(ConfigurationSaveMode.Modified);
It'll achieve the same result provided you have the right permissions, only you don't have to treat the config file as a generic XmlDocument
and parse the items to find the correct section.