Solved

Problems using MSBuild XMLUpdate and UpdateFiles

Posted on 2009-07-10
7
3,883 Views
Last Modified: 2013-12-12
Hi,
We have a large application and are trying to automate the build process between different environments.
I have an MSBuild script working quite well for the majority.
I have successfully got FileUpdate working in the "UpdateConnections" target (see below), and this loops through the .config files and .cs files etc replacing any instance it comes across.
It also does the connectionstrings in web.config which is great.

I'm now looking to tackle the web.config for other things.
We have a number of appSettings we need to change, and also have an identity we impersonate in system.web.

See below for an example of this web.config.

As you can see, we would like to update appVersion, and SCIUploadPath.
I have tried:

<XmlUpdate      
                  Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"            
                  XmlFileName="$(BuildDirectory)\web.config"
                  Xpath="/configuration/appSettings/add[@key='appVersion']/@value"
                  Value="154"
              />

But this didn't bring any luck.
In the log I just got:

<message level="normal"><![CDATA[Updating Xml Document "E:\Builds\build\PreProduction\Apps\web.config".]]></message>
      <message level="normal"><![CDATA[  0 node(s) selected for update.]]></message>
      <message level="normal"><![CDATA[XmlUpdate Wrote: "154".]]></message>

This implies it found the file etc no problem (correct path), but had a problem finding the node?

I've found exmaples online, but the Community Tasks stuff seems to be pretty porrly documented.



Also having a poor time using FileUpdate when trying to change the identity line.

             <FileUpdate Files="$(BuildDirectory)\web.config"
                          Regex="domain\user"
                          ReplacementText ="NEWDOMAIN\NEWUSER" />

Log shows:
      <message level="normal"><![CDATA[Updating File "E:\Builds\build\PreProduction\Apps\web.config".]]></message>
      <message level="normal"><![CDATA[  Replaced matches with "NEWDOMAIN\NEWUSER".]]></message>

But nothing has changed in the file.

Can anyone assist?
Code from MSBuild file: 
<Target Name="UpdateConnections" DependsOnTargets ="GetConnectionFiles">
  <Attrib Files="@(ConnectionFiles)" ReadOnly="false"/>
  <FileUpdate Files="@(ConnectionFiles)" 
      Regex="DEVELOPMENT_SERVER_DNS" 
      ReplacementText ="$(NewServer)" />
</Target>
 
 
Our web.config file: 
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
    <!--blah blah -->
  </configSections>
  <connectionStrings>
    <!--blah blah -->
  </connectionStrings>
    <compilation debug="true">
      <assemblies>
        <!--blah blah -->
      </assemblies>
    </compilation>
    <pages autoEventWireup="false" buffer="true" enableSessionState="true" viewStateEncryptionMode="Auto" enableViewStateMac="true">
      <controls>
        <!--blah blah -->
      </controls>
      <namespaces>
        <!--blah blah -->
      </namespaces>
    </pages>
    <identity impersonate="true" userName="domain\user" password="987654321" />
  </system.web>
  <location allowOverride="true" inheritInChildApplications="true">
    <appSettings>
      
      <add key="appVersion" value="v1.4.8" />
      <add key="SCIUploadPath" value="\\devserver\eprDocDump\" />
    </appSettings>
  </location>
</configuration>

Open in new window

0
Comment
Question by:Skidmerc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 500 total points
ID: 24821891
Your xpath is missing a level.

Your syntax looks correct. I just checked it against my usage of the same task. It should work. Have you triple checked that the referenced file is the right COPY of that file and that you're updating it BEFORE publishing the app, where the file is copied out into the bin fodler?

Using "FileUpdate" task from assembly "C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.dll".
Task "FileUpdate"
  Updating File "C:\BUILDS\Work\24\BuildType\..\Sources\Dev\Core\AssemblyInfo.cs".
    Replaced matches with "AssemblyVersion("2.0.0.0")]".
  Updating File "C:\BUILDS\Work\24\BuildType\..\Sources\Dev\Core\Tests\Properties\AssemblyInfo.cs".
    Replaced matches with "AssemblyVersion("2.0.0.0")]".
Done executing task "FileUpdate".
0
 
LVL 2

Author Comment

by:Skidmerc
ID: 24821976
Definitely updating the right file.
Well spotted, forgot about the location level.
However, still getting the 0 node(s) selected for read message
Using XMLRead just to output the value:

<XmlRead
            Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
                  XmlFileName="$(BuildDirectory)\web.config"
                  XPath="/configuration/location/appSettings/add[@key='appVersion']/@value">
                  <Output TaskParameter="Value" PropertyName="appVersion"/>
            </XmlRead>


Heard some people mention it's down to the xlmns at the top of web config and it may work without it?
0
 
LVL 2

Author Comment

by:Skidmerc
ID: 24822057
I can confirm the following code works it I remove the xmlns attribute from the top line in web.config:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
leaving it as
<configuration>

<XmlRead
                  Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
                  XmlFileName="$(BuildDirectory)\web.config"
                  XPath="configuration/location/appSettings/add[@key='appVersion']/@value">
                  <Output TaskParameter="Value" PropertyName="appVersion"/>
            </XmlRead>

However, i don't want to have to change my web.config for the whole solution, just to be able to do this.
There must be another way?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 500 total points
ID: 24822122
I'm just heading out on vacation NOW, but I will say that if you can't find resolution quickly, it's REALLY easy to oll your own custom task that you will then have complete control over. I whipped up some to update project and assemblyinfo.cs properties in my builds. Something to consider...
0
 
LVL 2

Author Comment

by:Skidmerc
ID: 24822170
That's exactly what I'm looking at just now.

Might try put together a custom WebConfigUpdate task of some sort that will be bespoke to our own processes.
0
 
LVL 2

Author Comment

by:Skidmerc
ID: 24823633
Sorted.

Bit of a back possibly - but it works.

<FileUpdate Files="$(BuildDirectory)\web.config"
                          Regex="xmlns%3D%22http://schemas.microsoft.com/.NetConfiguration/v2.0%22"
                          ReplacementText =" " />
      
      <!-- Update the app version -->
            <XmlUpdate      
                  XmlFileName="$(BuildDirectory)\web.config"
                  Xpath="/configuration/location/appSettings/add[@key='appVersion']/@value"
                  Value="v$(Major).$(Minor).$(Revision)"
              />


The first one remove the "xmlns" line from the configration.
This then lets the xpath work it's stuff for the rest of the XMLUpdates.
0
 
LVL 2

Author Closing Comment

by:Skidmerc
ID: 31601978
Helped give me pointers for future work i can do with the Community Tasks project source, and helped me narrow down what was going wrong with current way of working.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question