This project is read-only.

Insert Xml Node as first child instead of last?

Aug 25, 2010 at 10:41 PM


Is there a way to insert an xml node as the first child of the parent element instead of as its last child? I tried using <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement"....> but could not get it to insert before the already existing children.

Here's why I need this: I use Entreprise Library's environment overrides with the MergeConfiguration.exe command (namely for connection strings). The problem with this is that for some obscure reason, the <clear /> tag is removed by the merge command in the following section:

    <clear />
    <add name="app_db" connectionString="..." providerName="System.Data.SqlClient" />
    <add name="ApplicationServices" connectionString="..." providerName="System.Data.SqlClient" />

The <clear /> tag is necessary when an application in a sub-folder uses the same connection string name a the application in the root folder. I really don't understand why IIS inherits Web.Config files!?! But that another story.

I tried using MSBuild community task XmlMassUpdate but since the <clear /> tag does not have any attributes to use as a key, it won't work. I tried using MSBuild.ExtensionPack.Xml.XmlFile to add a <clear /> tag but it adds it at the end...

Can you provide any help on how I could achieve this ?



Aug 26, 2010 at 12:25 AM

I think I managed to do it using the following code, but there has to be another (simpler) way !

  <Target Name="MergeConfig">
      <MergeDeltaConfigEXE>"$(ProgramFiles)\Microsoft Enterprise Library 4.1 - October 2008\Bin\MergeConfiguration.exe"</MergeDeltaConfigEXE>
          <![CDATA[<?xml version='1.0'?>
          <xsl:stylesheet xmlns:xsl='' version='1.0'>
             <xsl:template match='/configuration'>
                   <xsl:copy-of select="connectionStrings/preceding-sibling::*"/>
                     <clear />
                     <xsl:copy-of select="connectionStrings/*"/>
                   <xsl:copy-of select="connectionStrings/following-sibling::*"/>
     <Move SourceFiles="$(OutputPath)Web.config" DestinationFiles="$(OutputPath)Web.premerge.config" />
     <Exec Command="$(MergeDeltaConfigEXE) $(OutputPath)Web.premerge.config $(SourceWebPhysicalPath)\Configurations\Web.$(Configuration).dconfig $(OutputPath)Web.config" />
     <Delete Files="$(OutputPath)Web.premerge.config" />

     <MSBuild.ExtensionPack.Xml.XmlTask TaskAction="Transform" XmlFile="$(OutputPath)Web.config" XslTransform="$(XslAppendClearTagToConnectionStringSection)" OutputFile="$(OutputPath)Web.config" Indent="true" />




Aug 26, 2010 at 4:02 PM

You can do this*


        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement" File="c:\a\d.xml" Element="clear" ParentElement="/connectionStrings" InsertBeforeXPath="/connectionStrings/add[@name='app_db']"/>





* in the August release which is due soon. You can compile the code yourself now if you would like to test it. I've added InsertBeforeXPath and InsertAfterXPath so you can control where the element goes. If you dont provide them then it gets appended to the ParentElement.