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

Dea*_*ean 12 xml msbuild msbuildcommunitytasks

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

<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>
Run Code Online (Sandbox Code Playgroud)

我试着替换fileNametarget

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

它报告无法找到任何要更新的内容,所以我的问题是我如何才能获得更新的文件名属性?


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


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

Chr*_*ers 21

第一个问题是xpath对于更新属性是不正确的,它当前正在查找具有名为"fileName"的属性的"目标"节点,而不是名为"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" />
Run Code Online (Sandbox Code Playgroud)