12

我正在使用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>

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 部分定义了自己的命名空间?


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

4

3 回答 3

21

第一个问题是更新属性的 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" />
于 2009-08-19T18:54:15.567 回答
4

此处表示命名空间的要求

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

你能更新任何其他属性吗?

于 2009-08-13T13:12:13.237 回答
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用来写出特殊字符$

于 2009-08-21T02:42:35.033 回答