When I run the following code on a server running ColdFusion 2018:
<cfsetting enablecfoutputonly="yes">
<cfxml variable="test">
<cfoutput>
<test>
áéíóú
</test>
</cfoutput>
</cfxml>
<cfset testString = ToString(test)>
<cfset testStringISO = Replace(testString, "UTF-8", "iso-8859-1")>
<cffile action="write" file="#AbsoluteFilesPath#test.xml" output="#testStringISO#" charset="iso-8859-1">
Where AbsoluteFilesPath is an absolute reference to a location on the server. The method I'm using to change the encoding of the XML is found here. The test.xml file looks like this when I open it on the server in Notepad++:
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
αινσϊ
</test>
The encoding of the file shows as "ISO 8859-7".
Interestingly, opening the file with VSCode on my local machine shows up like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
�����
</test>
Here, the encoding of the file shows as "UTF-8". Selecting the command "Reopen with encoding ISO 8859-1" within the editor shows the file as it should be:
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
áéíóú
</test>
I've tested this code, replacing "iso-8859-1" with "utf-16", and the results are the same.
Why is the file encoding inconsistent and not what I expected? How can I ensure the file is created with the correct encoding?


