Question

Is it possible to remove the commented lines from a web.config on build? xml transform is fine to remove some elements but I couldn't find any syntax to clean the comments from the file.

We are using TFS 2010 build server for our builds.

Was it helpful?

Solution 2

UPDATE: See actual and working answer below.


It's not possible to do with xml transformation.

But you can do it with your own console app or msbuild task. See example code here Remove XML comments using Visual Studio 2010 Web Config Transformation

OTHER TIPS

<add xdt:Transform="RemoveAll" xdt:Locator="XPath(//comment())" />

Put this node within the root node of your transform file. "XPath(//comment())" selects all XML comment nodes to delete.

I had a similar problem where I wanted to remove dev comments from the config files before I published to the web site. I wrote an app that will recursively remove comments from config files in the directory I specify on the command line. The sample below assumes YourCommentRemover will do the same.

I included the comment remover project as part of my solution and referenced it in the web app I plan to deploy. You can just add the executable as a reference if you want. Since I didn't want the comment remover to get deployed, I added a task to delete it from the bin directory where it was being staged for deploy, (ProjectDir)obj\$(Configuration)\Package\PackageTmp\.

Open your project file in a text editor (You can right-click on the project file in the solution explorer and select 'Edit Project File').

Go to the very end of the project file and insert the following before </Project>:

<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
    <Exec Command="$(ProjectDir)bin\YourCommentRemover $(ProjectDir)obj\$(Configuration)\Package\PackageTmp" />
    <Exec Command="del $(ProjectDir)obj\$(Configuration)\Package\PackageTmp\bin\YourCommentRemover.*" />
</Target>

This target will run before any files are copied to the web application location on publish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top