I built an XML object of type System.Xml.XmlDocument.
$scheme.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False XmlDocument System.Xml.XmlNode
I use the method save() to save it to a file.
$scheme.save()
This saves the file in format UTF-8 with BOM. The BOM causes issues with other scripts down the line.
When we open the XML file in Notepad++ and save it as UTF-8 (without the BOM), other scripts down the line don't have a problem. So I've been asked to save the script without the BOM.
The MS documentation for the save method states:
The value of the encoding attribute is taken from the XmlDeclaration.Encoding property. If the XmlDocument does not have an XmlDeclaration, or if the XmlDeclaration does not have an encoding attribute, the saved document will not have one either.
The MS documentation on XmlDeclaration lists encoding properties of UTF-8, UTF-16 and others. It does not mention a BOM.
Does the XmlDeclaration have an encoding property that leaves out the BOM?
PS. This behavior is identical in Powershell 5 and Powershell 7.
Save()
are you calling? The latter half of the question deals with theencoding
attribute of the<?xml
declaration, yet the part about fixing the problem by resaving the file in Notepad++ suggests the real problem is the text encoding of the file itself. For that you can create a UTF-8 non-BOMStreamWriter
with$encoding = New-Object -TypeName 'System.Text.UTF8Encoding' -ArgumentList $false; $writer = New-Object -TypeName 'System.IO.StreamWriter' -ArgumentList $outputPath, $shouldAppend, $encoding
and pass that toSave()
. – Lance U. Matthews