I\'m using web.config transformation as described in the below post in order to generate configs for different environments.
http://vishaljoshi.blogspot.com/2009/03/
Use the InsertIfMissing transformation to ensure that the appSetting exists.
Then use the Replace transformation to set its value.
You could also use the SetAttributes transformation instead of Replace. The difference is that SetAttributes does not touch child nodes.
These techniques are much better than remove+insert because existing nodes are not moved to the bottom of their parent node. New nodes are appended at the end. Existing nodes stay where they are in the source file.
This answer applies only to newer versions of Visual Studio (2012 or newer).