如何从 MSBuild 脚本更新 XML 属性?
我正在使用 MSBuild 和 MSBuild 社区任务(使用 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>
并且我尝试替换 target
的 fileName
<XmlUpdate XmlFileName="$(BuildDir)\Builds\%(Configuration.Identity)\_PublishedWebsites\Presentation\Web.config"
XPath="//configuration/nlog/targets/target[@fileName]"
Value="${logDirectory}\SomeLog_%(Configuration.Identity).log" />
它报告为无法找到任何要更新的内容,所以我的问题是如何获取 filename 属性要更新吗?
编辑:这可能是命名空间冲突的情况,因为 NLog 部分定义了自己的命名空间?
更新:发布的声明名称空间的答案不起作用。
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>
and I try to replace the fileName
of the target
<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?
EDIT: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个问题是 xpath 对于更新属性不正确,它当前正在寻找具有名为“fileName”属性的“target”节点,而不是名为“target”的节点的“fileName”属性。
你想要的xpath是:
/configuration/nlog/targets/target/@fileName
至于命名空间问题, Preet Sangha对此有正确的答案,您需要使用名称空间前缀,并且这也必须应用于每个子元素,因为它们都在该名称空间中。
最终的声明是:
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".
The xpath you want is:
/configuration/nlog/targets/target/@fileName
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.
The final statement being:
这里< /a> 它表示需要命名空间,
您可以更新任何其他属性吗?
Here it indicates the requirement of a namespace
can you update any other attribute?
要完成 keeperofthesoul 给出的答案(我认为你应该给他赏金顺便说一句)看一下:
这里我使用
%24
写出特殊字符$
。To complete the answer given by keeperofthesoul (I think you should give him the bounty btw) take a look at:
Here I'm using
%24
to write out the special character$
.