Using XMLFile to create multiple elements of the same name

Mar 11, 2010 at 1:26 PM

I was hoping to use XMLFile to modify the <connectionStrings> element of a web.config file. This element can be parent to multiple <add> elements. I can create the first <add> element with the AddElement action and then write attributes to that new element. However AddElement does not create another <add> element, and any attributes simply overwrite the ones written to the first <add> element that I created.

Is there a way to add multiple elements of the same name and then address (index?) the elements to add attributes?

Here's what I'm trying to do:

    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>RemoveElement</Action>
      <Element>connectionStrings</Element>
      <ParentElement>/configuration</ParentElement>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddElement</Action>
      <Element>connectionStrings</Element>
      <ParentElement>/configuration</ParentElement>
    </WebConfigConnectionStrings>
    
    <!-- First add element -->
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddElement</Action>
      <Element>add</Element>
      <ParentElement>/configuration/connectionStrings</ParentElement>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>connectionString</Key>
      <ValueToAdd>$(LoggingDatabaseConnectionString)</ValueToAdd>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>name</Key>
      <ValueToAdd>$(LoggingDBConnectionName)</ValueToAdd>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>providerName</Key>
      <ValueToAdd>$(Connection_ProviderName)</ValueToAdd>
    </WebConfigConnectionStrings>
    
    <!-- Second add element -->
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddElement</Action>
      <Element>add</Element>
      <ParentElement>/configuration/connectionStrings</ParentElement>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>connectionString</Key>
      <ValueToAdd>$(ProductionTrackingConnectionString)</ValueToAdd>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>name</Key>
      <ValueToAdd>$(ProductionTrackingDBConnectionName)</ValueToAdd>
    </WebConfigConnectionStrings>
    <WebConfigConnectionStrings Include="$(SolutionRoot)\path\Web.config">
      <Action>AddAttribute</Action>
      <Element>/configuration/connectionStrings/add</Element>
      <Key>providerName</Key>
      <ValueToAdd>$(Connection_ProviderName)</ValueToAdd>
    </WebConfigConnectionStrings>
    
  </ItemGroup>

  <Target Name="WebConnectionsEdit" Condition="'@(WebConfigConnectionStrings)'!='' and '$(SQLServerName)'!=''">
    <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="%(WebConfigConnectionStrings.Action)" 
                                       File="%(WebConfigConnectionStrings.Identity)" 
                                       Key="%(WebConfigConnectionStrings.Key)" 
                                       Value="%(WebConfigConnectionStrings.ValueToAdd)" 
                                       Element="%(WebConfigConnectionStrings.Element)" 
                                       ParentElement="%(WebConfigConnectionStrings.ParentElement)" 
                                       Condition="'%(WebConfigConnectionStrings.Identity)'!=''"/>

  </Target>