如何在MSBuild脚本中更新XML属性?

13

我正在使用MSBuildMSBuild Community Tasks(使用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部分定义了自己的名称空间?


更新:发布的回答声明命名空间无效。

3个回答

21
第一个问题是xpath对于更新属性不正确,它当前正在寻找具有名为"fileName"的属性的"target"节点,而不是名为"target"的a节点的"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" />

4

这里指出了命名空间的要求。

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

您能更新其他属性吗?


这很有帮助,但并没有解决手头的问题。我还有其他XMLUpdate任务,它们没有声明命名空间,但可以正确更新。 - Dean

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来表示特殊字符$


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接