首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XDT-Transform添加注释

使用XDT-Transform添加注释
EN

Stack Overflow用户
提问于 2013-01-18 21:45:23
回答 5查看 5.2K关注 0票数 10

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

Xml转换运行良好。但是我似乎找不到将注释从xml转换文件传递到最终文件的方法。

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

EN

回答 5

Stack Overflow用户

发布于 2014-04-02 21:12:02

我已经为你找到了一个可行的解决方案。

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

这意味着这不会带来你的评论

代码语言:javascript
复制
<appSettings>
    <!--My Secret Encryption Key-->
    <add key="ENCRYPT_KEY" value="hunter2" xdt:Transform="Insert" />
</appSettings>

但这将会

代码语言:javascript
复制
<appSettings xdt:Transform="Remove" />
<appSettings xdt:Transform="Insert" >
    <!--My Secret Encryption Key-->
    <add key="ENCRYPT_KEY" value="hunter2"/>
</appSettings>

其他任何东西都不需要转换,因为它完全复制了元素。

Upside:您可以获得您的评论,而不必在每个关键元素中都插入xdt:Transform="Insert"

如果总格式的改变还可以,那就太棒了。此外,它还需要重新创建整个部分,这可能会增加转换的大小。

票数 9
EN

Stack Overflow用户

发布于 2013-12-14 02:41:55

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

例如:

代码语言:javascript
复制
<appSettings>
<remove key="comment" value="" xdt:Transform="Insert"><!-- this comment will appear in the transformed config file! --></remove>
</appSettings>
票数 4
EN

Stack Overflow用户

发布于 2016-12-09 07:53:35

不写代码是不可能的。

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

这是我的CommentAppendCommentPrepend的例子,它接受注释文本作为输入参数,因为我认为如果不这样的话,Insert本身就不能工作,因为你放入的注释会被xdt:Transform="Insert"转换忽略,因为它是注释。

代码语言:javascript
复制
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

代码语言:javascript
复制
<security xdt:Transform="CommentPrepend(comment line 123)" >
</security>
<security xdt:Transform="CommentAppend(comment line 123)" >
</security>

和输出:

代码语言:javascript
复制
  <!--comment line 123--><security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="NWebsecConfig" />
      <add segment="Logs" />
    </hiddenSegments>
  </requestFiltering>
</security><!--comment line 123-->

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14400253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档