As in my recent work, I had to deal with some configuration transformations from C# code, I've created a nuget that can be used if you want to apply a transformation programmatically. To do that, you can install the ConfigurationTransformation package from the official nuget repository.
In order to use it, you can call the following from the package manager console...
PM> Install-Package ConfigurationTransformations
...and then just create an instance of the ConfigTransformer
class to apply the transformation.
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string destinationFile = @"C:\temp\resultweb.config";
TransformationResult transformationResult =
configTransformer.ApplyTransformation(sourceFile, transformationFile, destinationFile);
If you don't specify the destination file, the source file content will be changed with the result transformed content.
The other option is to get the result content without saving it to a file:
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string transformedContent = configTransformer.GetTransformedFileContent(sourceFile, transformationFile);
CodeProject