9

是否可以使用 Web Deploy 的Parameters.xml 系统将 XML 元素插入到我的 web.config 中?

XmlFile参数“ kind ”似乎最接近我的需要,但它的match属性只接受 XPath 查询,而且我似乎无法在我的 XPath 查询中指定不存在的元素。(或者更确切地说,我可以指定一个不存在的元素 - Web Deploy 只是忽略它。)具体来说,我想转换它:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

进入这个:

<configuration>
   <plugins>
      <add name="bleh">
        <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>

(不幸的是,我不能在 web.config 中预先存储一个空option元素,因为这个特定的插件系统不喜欢无法识别/空的选项。)

感谢您的任何想法!

4

4 回答 4

3

从 Web Deploy V3 开始,这些事情现在成为可能。见官方文档

下面是一个 parameters.xml 文件的示例,它将添加 newNode 到所有节点,包括目标 xml 文件中的根:

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="&lt;newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>
于 2016-01-27T12:29:10.263 回答
1

您是否考虑过使用configSource?这允许您将配置文件拆分为多个较小的文件。web.config 将来自:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

使用 configSource 对此:

<configuration>
   <plugins configSource="plugins.config"/>
</configuration>

缺点是在部署期间您不会获得用于编辑配置值的漂亮 UI。如果您无法使参数化起作用,则需要考虑这一点。如果安装 UI 是您所追求的,那么您可以为您的管理员编写一个可以创建和修改 plugin.xml 文件的编辑工具。

于 2011-05-01T21:51:40.070 回答
1

Xpath 只是一种用于 XML 文档的查询语言——它本身不能更改 XML 文档或创建新的 XML 文档

专门为转换 XML 文档而设计的语言称为 XSLT。

这是一个非常简短的 XSLT 转换,可以解决您的问题

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="add[@name='bleh']">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <option name="foo" value="bar"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<configuration>
    <plugins>
        <add name="bleh"/>
    </plugins>
</configuration>

产生了想要的正确结果

<configuration>
   <plugins>
      <add name="bleh">
         <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>
于 2011-04-26T13:38:25.427 回答
0

您可以只使用常规的 web.config 转换实践吗?http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

您可以执行下面的代码,它将用下面的代码替换您的所有部分。实际上,如果它是您的 Web.Release.config 文件,您的问题应该用您提供的部分替换整个部分。

Web.Release.config:

<plugins>
  <add name="bleh">
     <option name="foo" value="bar"/>
  </add>
</plugins>
于 2011-04-19T19:30:56.043 回答