我可以使用 Web Deploy 将元素插入到 web.config 中吗?

发布于 2024-11-02 14:22:33 字数 826 浏览 8 评论 0原文

是否可以使用 Web Deploy 的 Parameters.xml 系统

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

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

为:(

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

不幸的是,我无法预先存储网络.config 带有空的 option 元素,因为这个特定的插件系统不喜欢无法识别/空的选项。)

感谢您的任何想法!

Is it possible to insert an XML element into my web.config using Web Deploy's Parameters.xml system?

The XmlFile parameter "kind" appears to be the closest to what I need, but its match attribute only accepts an XPath query, and I don't appear to be able to specify a non-existent element in my XPath query. (Or rather, I can specify a non-existent element - Web Deploy just ignores it.) Specifically, I would like to transform this:

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

into this:

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

(Unfortunately I can't pre-stock the web.config with an empty option element because this particular plugin system doesn't like unrecognized/empty options.)

Thanks for any ideas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撕心裂肺的伤痛 2024-11-09 14:22:33

现在从 Web Deploy V3 开始,这样的事情就成为可能。
请参阅官方文档。

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

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="<newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>

Such things are now possible starting with the Web Deploy V3.
See the official documentation.

Here is one example of a parameters.xml file which will add newNode to all nodes including the root in target xml file:

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="<newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>
佞臣 2024-11-09 14:22:33

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>

Xpath is only a query language for XML documents -- it cannot by itself change an XML document or create a new XML document.

The language especially designed for transforming an XML document is called XSLT.

Here is a very short and simple XSLT transformation that solves your problem:

<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>

when this transformation is applied on the provided XML document:

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

the wanted, correct result is produced:

<configuration>
   <plugins>
      <add name="bleh">
         <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>
送你一个梦 2024-11-09 14:22:33

您是否考虑过使用 配置源?这允许您将配置文件分割成多个较小的文件。 web.config 将从:

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

使用 configSource 到此:

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

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

Have you considered using configSource? This allows you to split up your configuration files into multiple smaller files. The web.config would go from:

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

To this with configSource:

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

The downside is You won't get a nice UI for editing config values during deployment. It is something to consider if you cannot get parameterization to work. If the install UI is what you are after then you could code an editing tool for your admins that can create and modify the plugin.xml files..

昔日梦未散 2024-11-09 14:22:33

您可以使用常规的 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>

Can you just use the regular web.config transformation practices? http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

You can do the code below which will replace all of your section with the one below. Actually, what you have in your questions should replace your entire section with the one you provided if it was your Web.Release.config file.

Web.Release.config:

<plugins>
  <add name="bleh">
     <option name="foo" value="bar"/>
  </add>
</plugins>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文