【问题标题】:Adding comments using XDT-Transform使用 XDT-Transform 添加注释
【发布时间】:2013-01-18 13:45:23
【问题描述】:

我在 Visual Studio 2010 中使用 XDT-Transform 来生成多个配置文件。

Xml 转换工作正常。但我似乎无法找到将 cmets 从 xml 转换文件传输到最终文件的方法。

就像有Insert 转换用于添加配置设置一样,有没有添加cmets?如果没有 cmets,我可能不得不放弃整个变换方法。

【问题讨论】:

  • Vote 此功能将包含在 Visual Studio 中

标签: visual-studio-2010 xdt-transform


【解决方案1】:

我已经为您找到了可能的解决方案。

只需在您要添加的最高级别将某事物表示为插入。之后,您可以像往常一样添加元素。

意思是这不能带来你的评论

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

但这会

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

没有其他东西需要转换,因为它完全复制了元素。

优势:您可以获得 cmets,而不必在每个关键元素中插入 xdt:Transform="Insert"

缺点:您最终会完全破坏该部分并重新添加它,最终将其附加到 Web.config 的底部。如果总格式的变化没问题,那就太棒了。它还需要您重新创建整个部分,这可能会增加转换的大小。

【讨论】:

  • 如果您担心添加部分在文件中的位置,例如,您可以使用 InsertAfter 和 insertBefore 指定添加部分的位置。 xdt:Transform="InsertAfter(/configuration/appSettings)"
  • 您也可以表示 xdt:Transform="Replace" ,有时如果您尝试删除并插入最高级别,Visual Studio 会警告您无法删除最高级别。
【解决方案2】:

这并不完全是您想要的,但我偶尔会发现它很有用。 如果 cmets 包含在添加的元素中,XmlTransform 将添加它们。

例如

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

【讨论】:

    【解决方案3】:

    不写代码是不可能的。

    但是,我目前的解决方案是扩展 XDT 转换库,基本上是通过以下链接:Extending XML (web.config) Config transformation

    这是我的CommentAppendCommentPrepend 的示例,它们将注释文本作为输入参数,因为我相信否则Insert 本身无法工作,因为您放置xdt:Transform="Insert" 的注释将被忽略XDT 变换,因为它是评论。

    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);
            }
        }
    }
    

    还有输入web.Release.config

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

    然后输出:

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

    我目前正在使用 Reflector 来查看 Microsoft.Web.XmTransform 附带 Visual Studio V12.0 以了解它是如何工作的,但可能最好查看source code itself

    【讨论】:

    • 这是一个非常棒的解决方案;很高兴更深入地了解转换的工作原理。进一步的改进可能是在前置注释之后和附加注释之前添加一个回车。谢谢你
    【解决方案4】:

    据我所知,恐怕不可能使用 XDT-Transform 添加 cmets。

    至少XDT-Transform documentation中似乎没有提到它

    【讨论】:

      【解决方案5】:

      我建立了一个extension for XDT 来处理评论注入和其他相关任务。

      你可以试试online

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        相关资源
        最近更新 更多