Replace XML Element attributes in App.config

I have the following content in the app.config file, on which I am trying to update the ‘name’ attribute value in the “section” XML Element (structured XML under configuration/configSections)

<?xml version="1.0"?>
<configuration>
	<configSections>
		<section name="MailAccounts" type="MailboxWatcher.MailAccounts, MailboxWatcher" />
	</configSections>
	<appSettings>		
		<add key="FROMADDRESS" value="Receipts@help.com"/>
		<add key="SUCCESSSUBJECT" value="SUCCESS: Expense Receipts Received"/>
		<add key="REJECTEDSUBJECT" value="ERROR: Expense Report Has Not Been Submitted"/>
		<add key="REJECTEDSUBJECTNOMATCH" value="ERROR: Expense Report Number Invalid"/>
		<add key="REJECTEDSUBJECTBADFILE" value="ERROR: Upload failed"/>		
	</appSettings>
	<MailAccounts>
		<mailbox name="ExpenseReceipts" SiteUrl="https://sharepointdev.help.com/sites/Expense" Library="ExpenseReceipts" SmtpAddress="email.help.com" Port="995" UseSSL="true" User="ImageNowCrt" Password="E5th5t??">
			<IndexFields>
				<Field name="ReportID" type="SLT" dbcolumn="SHEET_ID" />
				<Field name="AssociateID" type="SLT" dbcolumn="EMPLID" />
				<Field name="AssociateName" type="SLT" dbcolumn="NAME" />
				<Field name="AssociateEmail" type="SLT" dbcolumn="EMAIL_ADDRESS" />
				<Field name="SubmissionDate" type="DT" dbcolumn="SUBMISSION_DATE" />
				<Field name="ReSubmissionDate" type="DT" dbcolumn="RESUBMISSION_DT" />
				<Field name="Pages" type="NUM" />
				<Field name="Title" type="SLT" />
				<Field name="IndexUser" type="SLT" />
				<Field name="IndexDate" type="DT" />
				<Field name="ScanUser" type="SLT" />
				<Field name="ScanDate" type="DT" />
				<Field name="NotificationSent" type="NUM" />
				<Field name="SubmitterEmail" type="SLT" />
			</IndexFields>
		</mailbox>
	</MailAccounts>
	<startup>
		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
	</startup>
</configuration>

My variable is defined as:
/*:configuration/*:configSections/*:section/@name
with a value of my name, NANDINI . If I plug this XPath and XML into an XPath tester, it works fine, but the value is never updated when I deploy my project.

I’ve fought with this for about 3 hours now, trying every single dumb XPath trick I can think of and absolutely nothing is working for me.

Hi @11nandini94,

First of all - welcome to the community, and thanks for reaching out!

If you’ve enabled Structured Configuration Variables in the process step:

And successfully targetted your XML:

You should be able to replace the “name” value within configuration/configSections/section with the name value “MailAccounts” with the following variable name:
/configuration/configSections/section[@name='MailAccounts']/@name

When I ran this in a test against your XML with a value Adam-MailAccounts-Test like so:

Octopus replaced the name section inside that tag with the value of that project variable:
image

I hope this helps! If you have any questions or run into any issues in implementing this, please get back in touch, and I’ll do my best to assist further.

Kind Regards,
Adam

Thank you adam,

I am using Octopus 2019.9.4 version and I don’t see the Structured Configuration Variables hence I tried with Json Configuration Variables. Even that is not working as expected.

PS: I wouldn’t be able to upgrade to the latest version at this moment. Could you please help if there is any alternate solution for my current version.

Thanks in Advance,
Nandini

Hi @11nandini94,

Thank you for getting back to us.

In Octopus Server 2019.9.4, the Structured Configuration Variables feature was not yet available (I believe this came out in 2020.4.X). This leaves a couple of alternate options that will work a little differently than Adam’s example.

Option 1: .NET Configuration transforms
This option performs the XML transforms based on a transformation file modifying a target configuration file.

Option 2: Substitute Variables in Files (aka “Substitute variables in templates”)
With this option, you’ll be implementing simple variable replacement using the Octostache format (example: #{variablename}).

Since you are currently unable to upgrade, I hope one of these options works for you.

Let us know if you have any additional questions.

Best Regards,
Donny