如何从 MSBuild 脚本更新 XML 属性?

发布于 2024-08-01 16:30:24 字数 1352 浏览 6 评论 0原文

我正在使用 MSBuildMSBuild 社区任务(使用 XMLUpdate 和 XMLMassUpdate) 来更新我的 Web.config 的各个部分,但有一件事让我难住了。 如果我有:

<configuration>
    <nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <targets>
            <target name="file" xsi:type="File" fileName="${logDirectory}\SomeLog.log" layout="${message}"/>
        </targets>
    </nlog> 
</configuration>

并且我尝试替换 targetfileName

<XmlUpdate XmlFileName="$(BuildDir)\Builds\%(Configuration.Identity)\_PublishedWebsites\Presentation\Web.config"
           XPath="//configuration/nlog/targets/target[@fileName]"
           Value="${logDirectory}\SomeLog_%(Configuration.Identity).log" />

它报告为无法找到任何要更新的内容,所以我的问题是如何获取 filename 属性要更新吗?


编辑:这可能是命名空间冲突的情况,因为 NLog 部分定义了自己的命名空间?


更新:发布的声明名称空间的答案不起作用。

I am using MSBuild and MSBuild Community Tasks (using XMLUpdate and XMLMassUpdate) to update various sections of my Web.config one thing has me stumped though. If I have:

<configuration>
    <nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <targets>
            <target name="file" xsi:type="File" fileName="${logDirectory}\SomeLog.log" layout="${message}"/>
        </targets>
    </nlog> 
</configuration>

and I try to replace the fileName of the target

<XmlUpdate XmlFileName="$(BuildDir)\Builds\%(Configuration.Identity)\_PublishedWebsites\Presentation\Web.config"
           XPath="//configuration/nlog/targets/target[@fileName]"
           Value="${logDirectory}\SomeLog_%(Configuration.Identity).log" />

It reports as being unable to find anything to update, so my question is how can I get the filename attribute to updated?


EDIT: Could this be a case of namespace clashes as the NLog section defines its own namespace?


UPDATE: The posted answer declaring the name space does not work.

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

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

发布评论

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

评论(3

百变从容 2024-08-08 16:30:24

第一个问题是 xpath 对于更新属性不正确,它当前正在寻找具有名为“fileName”属性的“target”节点,而不是名为“target”的节点的“fileName”属性。

你想要的xpath是:
/configuration/nlog/targets/target/@fileName

至于命名空间问题, Preet Sangha对此有正确的答案,您需要使用名称空间前缀,并且这也必须应用于每个子元素,因为它们都在该名称空间中。

最终的声明是:

<XmlUpdate
  Prefix="n"
  Namespace="http://www.nlog-project.org/schemas/NLog.xsd"
  XmlFileName="output.xml"
  XPath="//configuration/n:nlog/n:targets/n:target/@fileName"
  Value="${logDirectory}\UpdateWorked.log" />

The first problem is the xpath is incorrect for updating the attribute, it is currently looking for "target" nodes with an attribute called "fileName" rather than the "fileName" attribute of the a node called "target".

The xpath you want is:
/configuration/nlog/targets/target/@fileName

As for the namespace issue, Preet Sangha has the correct answer for that, you need to use the namespace prefix, and this must be applied to every sub-element as well, since they are all in that namespace.

The final statement being:

<XmlUpdate
  Prefix="n"
  Namespace="http://www.nlog-project.org/schemas/NLog.xsd"
  XmlFileName="output.xml"
  XPath="//configuration/n:nlog/n:targets/n:target/@fileName"
  Value="${logDirectory}\UpdateWorked.log" />
躲猫猫 2024-08-08 16:30:24

这里< /a> 它表示需要命名空间,

<XmlUpdate
   Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName ....

您可以更新任何其他属性吗?

Here it indicates the requirement of a namespace

<XmlUpdate
   Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
   XmlFileName ....

can you update any other attribute?

岁月打碎记忆 2024-08-08 16:30:24

要完成 keeperofthesoul 给出的答案(我认为你应该给他赏金顺便说一句)看一下:

<XmlUpdate
  XmlFileName="web.config"
  XPath="//configuration/x:nlog/x:targets/x:target/@fileName"
  Value="%24{logDirectory}\SomeLog_%(Configuration.Identity).log"
  Prefix="x"
  Namespace="http://www.nlog-project.org/schemas/NLog.xsd"
  />

这里我使用 %24 写出特殊字符 $

To complete the answer given by keeperofthesoul (I think you should give him the bounty btw) take a look at:

<XmlUpdate
  XmlFileName="web.config"
  XPath="//configuration/x:nlog/x:targets/x:target/@fileName"
  Value="%24{logDirectory}\SomeLog_%(Configuration.Identity).log"
  Prefix="x"
  Namespace="http://www.nlog-project.org/schemas/NLog.xsd"
  />

Here I'm using %24 to write out the special character $.

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