如何从MSBuild脚本更新XML属性? [英] How do I update an XML attribute from an MSBuild script?

查看:118
本文介绍了如何从MSBuild脚本更新XML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MSBuild

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>

,而我尝试替换targetfileName

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

编辑:在NLog部分定义其自己的名称空间时,是否可能发生名称空间冲突?

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.

推荐答案

第一个问题是xpath无法更新属性,它当前正在寻找具有"fileName"属性而不是"fileName"属性的"target"节点名为目标"的节点的fileName"属性.

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

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

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

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

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.

最后的陈述是:

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

这篇关于如何从MSBuild脚本更新XML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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