How do I update an XML attribute from an MSBuild script?

后端 未结 3 514
臣服心动
臣服心动 2021-02-14 00:08

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:

相关标签:
3条回答
  • 2021-02-14 00:28

    To complete the answer given by keeperofthesoul (I think you should give him the bounty btw) take a look at:

    <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"
      />
    

    Here I'm using %24 to write out the special character $.

    0 讨论(0)
  • 2021-02-14 00:36

    Here it indicates the requirement of a namespace

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

    can you update any other attribute?

    0 讨论(0)
  • 2021-02-14 00:37

    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:

    <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" />
    
    0 讨论(0)
提交回复
热议问题