代码之家  ›  专栏  ›  技术社区  ›  Dean

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

  •  12
  • Dean  · 技术社区  · 17 年前

    我正在使用 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 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 回复  |  直到 15 年前
        1
  •  21
  •   Community Mohan Dere    9 年前

    第一个问题是xpath更新属性时不正确,它当前正在查找具有名为“filename”的属性的“target”节点,而不是名为“target”的节点的“filename”属性。

    您需要的xpath是: /配置/nlog/targets/target/@filename

    至于名称空间问题, 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" />
    
        2
  •  4
  •   Preet Sangha    17 年前

    Here 它指示命名空间的要求

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

    你能更新其他属性吗?

        3
  •  3
  •   Community Mohan Dere    9 年前

    完成以下人员给出的答案: 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 写出这个特殊的字符 $ .