Skip to content

System.Xml.XmlDocument.Save() unexpectedly creates UTF-8 files *with BOM* #28218

@mklement0

Description

@mklement0

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:

239
187
191

which are the byte values of the UTF-8 BOM (0xef 0xbb 0xbf)

Metadata

Metadata

Assignees

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions