Adding Comments Using XDT-Transform

I am using XDT-Transform in Visual Studio 2010 to create multiple configuration files.

Xml conversion works fine. But I can not find a way to transfer comments from the xml conversion file to the destination file.

Just as there is Insert transform to add configuration settings, is there anyway to add comments? Without comment, I may have to abandon the whole approach to transformation.

+6
source share
4 answers

I have found a possible solution for you.

It is only necessary to designate something as an insert at the highest level that you add. After that, you can simply add items as usual.

This means that it will not work to display a comment.

 <appSettings> <!--My Secret Encryption Key--> <add key="ENCRYPT_KEY" value="hunter2" xdt:Transform="Insert" /> </appSettings> 

But it will be

 <appSettings xdt:Transform="Remove" /> <appSettings xdt:Transform="Insert" > <!--My Secret Encryption Key--> <add key="ENCRYPT_KEY" value="hunter2"/> </appSettings> 

Nothing else needs to be converted, since it completely copies the element.

Surface . You get your comments and should not embed xdt:Transform="Insert" in each key element.

Downside : you completely destroy the section and add it, which ends adding it to the end of your Web.config. If changing the full formatting is ok, then it's awesome. It also requires that you recreate the entire section, which could increase the size of your transformations.

+5
source

This is not exactly what you want, but I find it useful from time to time. XmlTransform will add comments if they are contained in the added element.

eg.

 <appSettings> <remove key="comment" value="" xdt:Transform="Insert"><!-- this comment will appear in the transformed config file! --></remove> </appSettings> 
+2
source

As far as I know, adding comments using XDT-Transform is not possible, I'm afraid.

At least this is not mentioned in the XDT-Transform documentation

0
source

Impossible without writing code.

However, my current solution is to extend the XDT Transform library, mainly following the link: XML Extension (web.config) Configuration Conversion

And here is my CommentAppend example, CommentPrepend , which takes the comment text as an input parameter, since I believe that otherwise Insert itself cannot work as a comment that you would put on your xdt:Transform="Insert" , XDT will be ignored Transform as it is a comment.

 internal class CommentInsert: Transform { protected override void Apply() { if (this.TargetNode != null && this.TargetNode.OwnerDocument != null) { var commentNode = this.TargetNode.OwnerDocument.CreateComment(this.ArgumentString); this.TargetNode.AppendChild(commentNode); } } } internal class CommentAppend: Transform { protected override void Apply() { if (this.TargetNode != null && this.TargetNode.OwnerDocument != null) { var commentNode = this.TargetNode.OwnerDocument.CreateComment(this.ArgumentString); this.TargetNode.ParentNode.InsertAfter(commentNode, this.TargetNode); } } } 

And input web.Release.config :

 <security xdt:Transform="CommentPrepend(comment line 123)" > </security> <security xdt:Transform="CommentAppend(comment line 123)" > </security> 

And the conclusion:

  <!--comment line 123--><security> <requestFiltering> <hiddenSegments> <add segment="NWebsecConfig" /> <add segment="Logs" /> </hiddenSegments> </requestFiltering> </security><!--comment line 123--> 

I am currently using Reflector to view Microsoft.Web.XmTransform comes with Visual Studio V12.0 to figure out how this works, but it's probably best to look at the source code itself

0
source

All Articles