How to specify insert location for node within config install xdt transform

1.5k views Asked by At

I have a nuget package I'm trying to create and can't seem to figure out how to make this last part work properly. I'm using the config.install.xdt transforms in order to add configuration elements to the client config file.

I am simply adding a new <section> node into the client config file, as shown below:

<?xml version="1.0" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <configSections xdt:Transform="InsertIfMissing">
        <section xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"
                 name="myPackageName" 
                 type="MyPackage.Config.MySection, MyPackage" />
    </configSections>
</configuration>

The issue is that <configSections> must be the first child of the root <configuration> element in the client app.config. If there is no existing <configSections> in the client application, the above transform just appends <configSections> as the last child within <configuration>.

Is there any way to force <configSections> to be inserted as the first child within <configuration>?

Edit 1

I wanted to add some details as to what I've tried and with what outcomes...

The first transform I went with was <configSections xdt:Transform="InsertIfMissing">. Even when this is the first child in my config.install.xdt file, it gets placed towards the end of the client config file upon install.

I've tried several variations with the transforms InsertBefore and InsertAfter. Unfortunately I can't just use, for example, <configSections xdt:Transform="InsertBefore(/configuration/appSettings)" because an appSettings element may not always exist in the client config file and also may not be the first child node.

I think there must be some functionality for this since when installing a nuget package like Entity Framework into a project with a config file that doesn't already have a <configSections> node, the <configSections> node is added as the first child within the <configuration> root with the install of Entity Framework.

Edit 2

After hours of searching and banging my head against a wall, through Leo's answer I found another stackoverflow post with generally the same question. I flagged my question as a duplicate. Here is the link.

1

There are 1 answers

1
Leo Liu On BEST ANSWER

How to specify insert location for node within config install xdt transform

The <configSections> should be the first child of the root element in the client app.config when you install the nuget to the project by default.

Just as you know and as specified in the documentation:

If this element is in a configuration file, it must be the first child element of the element

It's where you specify what configuration sections are going to be in your app.config, hence, it has to be at the start of the configuration element.

So the <configSections> should be the first child of the root element in the client app.config by default, even if there is no existing <configSections> in the client application.

As test, I have created a simple test nuget package with a content folder, which including App.config.transform and Web.config.transform. Copy your code into the contents of .transform:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <configSections xdt:Transform="InsertIfMissing">
    <section xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"
             name="myPackageName"
             type="MyPackage.Config.MySection, MyPackage" />
  </configSections>
</configuration>

Then install this nuget package to the test project:

enter image description here

Besides, you can try to use wildcard * instead of appSettings when you use the the transforms InsertBefore, like:

<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />

Hope this helps.