Unfortunately, the presence of an explicit encoding="utf-8"
attribute in the declaration of an XML document causes .NET's [xml]
(System.Xml.XmlDocument
) type to .Save() https://learn.microsoft.com/en-US/dotnet/api/System.Xml.XmlDocument.Save the document, when given a file path, to an UTF-8-encoded file with BOM, which can indeed cause problems (even though it shouldn't[1]).
请求更改此设置 https://github.com/dotnet/runtime/issues/28218已经原则上开绿灯, 但是截至 .NET 6.0 尚未实现(由于关于改变的更大讨论[System.Text.Encoding]::UTF8
to not在这种情况下使用 BOM.Save()
也将不再自动创建 BOM)。
有点讽刺的是,the absence of an encoding
属性原因.Save()
创建 UTF-8 编码的文件without a BOM.
A simple solution is therefore to remove the encoding attribute[2]; e.g.:
# Create a sample XML document:
$xmlDoc = [xml] '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>'
# Remove the 'encoding' attribute from the declaration.
# Without this, the .Save() method below would create a UTF-8 file *with* BOM.
$xmlDoc.ChildNodes[0].Encoding = $null
# Now, saving produces a UTf-8 file *without* a BOM.
$xmlDoc.Save("$PWD/out.xml")
[1] Per the XML W3C Recommendation https://www.w3.org/TR/xml/#charencoding: "entities encoded in UTF-8 MAY begin with the Byte Order Mark" [BOM].
[2] This is safe to do, because the XML W3C Recommendation https://www.w3.org/TR/xml/#charencoding effectively mandates UTF-8 as the default in the absence of both a BOM and an encoding
attribute.