加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:41:54 所属栏目:百科 来源:网络整理
导读:我正在使用 MSBuild和 MSBuild Community Tasks(使用XMLUpdate和 XMLMassUpdate)来更新我的Web.config的各个部分,但有一件事情让我失望.如果我有: configuration nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:
我正在使用 MSBuild和 MSBuild 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>

我尝试替换目标的fileName

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

它报告为无法找到任何更新,所以我的问题是如何获取文件名属性更新?

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读