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

时间:2009-08-13 13:06:08

标签: xml msbuild msbuildcommunitytasks

我正在使用MSBuildMSBuild Community Tasks(使用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>

我尝试替换fileName

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

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


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


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

3 个答案:

答案 0 :(得分:21)

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

你想要的xpath是: /配置/ n日志/目标/目标/ @文件名

至于名称空间问题Preet Sangha has the correct answer for that,您需要使用名称空间前缀,这也必须应用于每个子元素,因为它们都在该名称空间中。

最终陈述是:

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

答案 1 :(得分:4)

Here表示命名空间的要求

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

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

答案 2 :(得分:3)

要完成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来写出特殊字符$