System.Xml.XmlDocument.Save(), when given a file path to write to, unexpectedly creates UTF-8 encoded files with BOM if the document's XML declaration has an encoding="UTF-8" attribute.
(By contrast, the absence of the encoding attribute or absence of an XML declaration altogether causes a BOM-less UTF-8 file to be created, as expected.)
Repro in PowerShell Core (on any supported platform; PowerShell's [xml] cast instantiates a System.Xml.XmlDocument from the specified XML string):
([xml] '<?xml version="1.0" encoding="UTF-8" ?><foo />').Save("$HOME/t.xml")
Get-Content -AsByteStream $HOME/t.xml | Select-Object -First 3
The above yields:
which are the byte values of the UTF-8 BOM (0xef 0xbb 0xbf)