Search code examples
.netnant

How to change connection string automatically on build with Nant


is it possible to change the connection string in my web.config automatically when build type is release using Nant? if so, how? thanks


Solution

  • I think you could use the xmlpoke task. For example, if your web.config is

    <?xml version="1.0"?>
    <configuration>
        <connectionStrings>
            <add name="myDb" connectionString="blah" providerName="blah"/>
        </connectionStrings>
    </configuration>
    

    Then you could add a task to your build file like this.

    <xmlpoke 
        file="path_to_your_web_root\Web.config"
        xpath="/configuration/connectionStrings/add[@name='myDb']/@connectionString"
        value="your_connection_string" />
    

    Oh, here is the documentation of the xmlpoke task. http://nant.sourceforge.net/release/latest/help/tasks/xmlpoke.html