在Visual Studio中不是Web项目的项目的App.Config转换? [英] App.Config Transformation for projects which are not Web Projects in Visual Studio?

查看:169
本文介绍了在Visual Studio中不是Web项目的项目的App.Config转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于基于Visual Studio 2010 Web的应用程序,我们具有配置转换功能,通过这些功能,我们可以为不同的环境维护多个配置文件。但是该功能不适用于Windows Services / WinForms或控制台应用程序的App.Config文件。

For Visual Studio 2010 Web based application we have Config Transformation features by which we can maintain multiple configuration files for different environments. But the same feature is not available for App.Config files for Windows Services/WinForms or Console Application.

此处提供了一种解决方法:将XDT魔术应用于App.Config

There is a workaround available as suggested here: Applying XDT magic to App.Config.

但是,它并不简单,需要很多步骤。

However it is not straightforward and requires a number of steps. Is there an easier way to achieve the same for app.config files?

推荐答案

此方法现在可以与在以下位置处理的Visual Studio AddIn一起使用:本文: SlowCheetah-Web.config转换语法现在可用于任何XML配置文件。 / p>

This works now with the Visual Studio AddIn treated in this article: SlowCheetah - Web.config Transformation Syntax now generalized for any XML configuration file.


您可以右键单击web.config,然后单击添加配置
转换。完成此操作后,您将获得一个web.debug.config和一个
web.release.config。您可以根据需要将web.whatever.config设置为
,只要名称与配置配置文件对齐即可。这些文件
只是您要进行的更改,而不是
web.config的完整副本。

You can right-click on your web.config and click "Add Config Transforms." When you do this, you'll get a web.debug.config and a web.release.config. You can make a web.whatever.config if you like, as long as the name lines up with a configuration profile. These files are just the changes you want made, not a complete copy of your web.config.

您可能会认为自己想要使用XSLT转换web.config,但是
虽然直观上感觉很冗长。

You might think you'd want to use XSLT to transform a web.config, but while they feels intuitively right it's actually very verbose.

这里有两个转换,一个使用XSLT且相同一种使用XML
文档转换语法/命名空间。与所有事物一样,XSLT中有
多种方法可以做到这一点,但您可以理解。 XSLT
是一种通用的树转换语言,而此部署
则针对常见场景的特定子集进行了优化。但是,
最酷的部分是每个XDT转换都是.NET插件,因此您可以自己制作

Here's two transforms, one using XSLT and the same one using the XML Document Transform syntax/namespace. As with all things there's multiple ways in XSLT to do this, but you get the general idea. XSLT is a generalized tree transformation language, while this deployment one is optimized for a specific subset of common scenarios. But, the cool part is that each XDT transform is a .NET plugin, so you can make your own.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
  <xsl:copy>           
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/configuration/appSettings">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    <xsl:element name="add">
      <xsl:attribute name="key">NewSetting</xsl:attribute>
      <xsl:attribute name="value">New Setting Value</xsl:attribute>
    </xsl:element>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

或者通过部署转换进行同样的操作:

Or the same thing via the deployment transform:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <appSettings>
      <add name="NewSetting" value="New Setting Value" xdt:Transform="Insert"/>
   </appSettings>
</configuration>


这篇关于在Visual Studio中不是Web项目的项目的App.Config转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆