我使用Visual Studio 2010中XDT-變換生成多個配置文件XDT-變換。添加註釋使用
XML轉換工作正常。但我似乎無法找到從XML轉換文件到最終文件的評論。
就像有Insert
變換添加的配置設置,反正是有添加註釋?如果沒有意見,我可能不得不放棄整個變革方法。
我使用Visual Studio 2010中XDT-變換生成多個配置文件XDT-變換。添加註釋使用
XML轉換工作正常。但我似乎無法找到從XML轉換文件到最終文件的評論。
就像有Insert
變換添加的配置設置,反正是有添加註釋?如果沒有意見,我可能不得不放棄整個變革方法。
至於我使用XDT-變換是知道添加註釋是不可能的,我害怕。
至少它不會出現被內XDT-Transform documentation
提到這不正是你想要的,但我覺得它在一段有用的一次。 如果XmlTransform包含在添加的元素中,它將添加註釋。
例如
<appSettings>
<remove key="comment" value="" xdt:Transform="Insert"><!-- this comment will appear in the transformed config file! --></remove>
</appSettings>
我找到了一個可能的解決方案。
只需要將某個東西表示爲正在添加的最高級別的插入。之後,您可以像往常一樣添加元素。
含義,這並不工作帶來了您的評論
<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>
沒有別的要求對元素的變換,因爲它完全複製。
上行:您收到您的評論,並且不必將xdt:Transform="Insert"
插入到每個關鍵元素中。
缺點:你最終完全破壞該部分並重新添加它,最終將其附加到Web.config的底部。如果總格式的改變是好的,那麼真棒。此外,它還要求您重新創建可以增加變換大小的整個部分。
很好的知道下降 –
洛爾的缺點是一種很大的缺點... –
如果你擔心它們出現在文件中的位置,你可以使用InsertAfter和insertBefore來指定你添加的部分的位置,例如XDT:變換= 「InsertAfter(/配置/的appSettings)」 – Rob
無需編寫代碼不可能的。
不過,我目前的解決方案是程度XDT轉換庫,基本上通過下面的鏈接:Extending XML (web.config) Config transformation
這裏是我的CommentAppend
,CommentPrepend
內搭評論文本作爲輸入參數,因爲我相信,否則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-->
我目前使用反射來看看Microsoft.Web.XmTransform
自帶的Visual Studio V12.0弄清楚它是如何工作的,但可能最好看一下source code itself
[Vote](https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/2578637-allow-inserting-comments-with-web-config-transform)將此功能包含在Visual工作室 –