Removing comments from web.config on build
Asked Answered
H

3

6

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.

Hectograph answered 6/2, 2014 at 1:3 Comment(1)
Why do you even want to do this?Verdin
P
6

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

Pilotage answered 6/2, 2014 at 1:21 Comment(1)
Yes it possible, see correct answer belowIdealism
G
13
<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.

Gurdwara answered 19/2, 2021 at 10:21 Comment(1)
I can confirm it works and this should be the correct answer.Idealism
P
6

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

Pilotage answered 6/2, 2014 at 1:21 Comment(1)
Yes it possible, see correct answer belowIdealism
P
0

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.

Prase answered 25/1, 2018 at 2:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.