我在Visual Studio2010中使用XDT-Transform来生成多个配置文件。
Xml转换运行良好。但是我似乎找不到将注释从xml转换文件传递到最终文件的方法。
就像有用于添加配置设置的Insert转换一样,还有添加注释的功能吗?如果没有注释,我可能不得不放弃整个转换方法。
发布于 2014-04-02 21:12:02
我已经为你找到了一个可行的解决方案。
只需要在您添加它的最高级别将某件东西表示为插入。在此之后,您可以像往常一样添加元素。
这意味着这不会带来你的评论
<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>其他任何东西都不需要转换,因为它完全复制了元素。
Upside:您可以获得您的评论,而不必在每个关键元素中都插入xdt:Transform="Insert"。
如果总格式的改变还可以,那就太棒了。此外,它还需要重新创建整个部分,这可能会增加转换的大小。
发布于 2013-12-14 02:41:55
这并不完全是您想要的,但我偶尔会发现它很有用。如果注释包含在添加的元素中,XmlTransform将添加注释。
例如:
<appSettings>
<remove key="comment" value="" xdt:Transform="Insert"><!-- this comment will appear in the transformed config file! --></remove>
</appSettings>发布于 2016-12-09 07:53:35
不写代码是不可能的。
然而,我目前的解决方案是扩展XDT转换库,基本上是通过以下链接:Extending XML (web.config) Config transformation
这是我的CommentAppend,CommentPrepend的例子,它接受注释文本作为输入参数,因为我认为如果不这样的话,Insert本身就不能工作,因为你放入的注释会被xdt:Transform="Insert"转换忽略,因为它是注释。
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查看Visual StudioV12.0附带的Microsoft.Web.XmTransform,以了解它是如何工作的,但可能更好的方法是查看source code itself
https://stackoverflow.com/questions/14400253
复制相似问题