Adding comments using XDT-Transform

≡放荡痞女 提交于 2019-12-08 16:08:51

问题


I am using XDT-Transform in Visual Studio 2010 to generate multiple config files.

Xml transformation is working fine. But I cannot seem to find way to carry comments from an xml transform file to final file.

Just like there is Insert transform for adding config settings, is there anyway to add comments? Without comments I may have to give up on the whole transform approach.


回答1:


I've found a possible solution for you.

It is only necessary to denote something as being an Insert at the highest level you are adding it. After that you may just add elements like you would normally.

Meaning this doesn't work to bring over your comment

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

But this would

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

Nothing else requires a transform as it copies over the element entirely.

Upside: You get your comments and don't have to insert xdt:Transform="Insert" into every key element.

Downside: You end up destroying the section entirely and re add it which ends up appending it to the bottom of your Web.config. If the change in total formatting is okay then awesome. Also it requires you recreate the entire section which could increase the size of your transforms.




回答2:


Not possible without writing code.

However, my current solution is to extent the XDT Transform library, by basically following the link: Extending XML (web.config) Config transformation

And here is my example of CommentAppend, CommentPrepend which take comment text as input parameter, as I believe otherwise Insert itself can't work as the comment you would put your xdt:Transform="Insert" will be ignored by XDT Transform as it is 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 the input web.Release.config:

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

And the output:

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

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




回答3:


This isn't exactly what you want, but I find it useful once in a while. XmlTransform will add comments if they are contained in an added element.

e.g.

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



回答4:


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

At least it doesn't appear to be mentioned within the XDT-Transform documentation



来源:https://stackoverflow.com/questions/14400253/adding-comments-using-xdt-transform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!