Remove and add connection string I want to remove a connection string that may already have been set in a different config file and then set it again.
Say my application has a Web.config file and a Web.Debug.config file then I can achieve what I want to do in the following way:
Web.config:
  &lt;connectionStrings&gt;
    &lt;remove name=&quot;MyConnectionString&quot; /&gt;
    &lt;add name=&quot;MyConnectionString&quot; connectionString=&quot;...&quot;;User Id=******;Password=******;&quot; /&gt;
  &lt;/connectionStrings&gt;

Web.Debug.config
  &lt;connectionStrings&gt;
    &lt;remove xdt:Transform=&quot;Replace&quot; name=&quot;MyConnectionString&quot; /&gt;
    &lt;add
      xdt:Transform=&quot;SetAttributes(connectionString)&quot;
      xdt:Locator=&quot;Condition(@name='MyConnectionString')&quot;
      connectionString=&quot;...&quot;;User Id=******;Password=******;&quot;
    /&gt;
  &lt;/connectionStrings&gt;

I am going to be removing connection strings and then adding them to ensure there are no errors that result from a connection string already having been set in some other config file. Before I change tens or hundreds of config files across all of our repositories I want to know if there is a better way - easier to read or requiring fewer/simpler changes.
Each add entry in connectionStrings section will be parsed as ConnectionStringSettings at the end of the day.
This class has 3 properties: Name,ConnectionString and ProviderName.
In your provided sample you have defined only the first two.
Your provided sample seems to me a bit too specific/strict:

Replace only the connectionStrings attribute
Match the entity where the name equals MyConnectionString

The same result could be achieved with less specific transformation rules:
&lt;connectionStrings&gt;
    &lt;remove xdt:Transform=&quot;Replace&quot; name=&quot;MyConnectionString&quot; /&gt;
    &lt;add name=&quot;MyConnectionString&quot; connectionString=&quot;...;User Id=******;Password=123;&quot; 
      xdt:Transform=&quot;SetAttributes&quot; xdt:Locator=&quot;Match(name)&quot; /&gt;
  &lt;/connectionStrings&gt;


I've specified the name as an attribute of the add element
I've changed the Locator attribute value from Condition to Match
And I've removed the (connectionString) suffix from the Transform attribute

I've checked the transformation with this tool.