Skip to content

Instantly share code, notes, and snippets.

@maskati
Last active January 9, 2025 07:06
Show Gist options
  • Save maskati/69324c8232cc93914b65b8b300a60dc5 to your computer and use it in GitHub Desktop.
Save maskati/69324c8232cc93914b65b8b300a60dc5 to your computer and use it in GitHub Desktop.
PowerShell XML with and without BOM

PowerShell XML with and without BOM

# ok, returns XmlDocument
Invoke-RestMethod 'https://gist.githubusercontent.com/maskati/69324c8232cc93914b65b8b300a60dc5/raw/a082827c9cb6faacd9f89a5c803d91daef3d1b3e/nobom.xml'

# ok, conversion to XmlDocument fails and returns string with initial BOM character (0xFEFF) UTF8 encoded as 0xEF 0xBB 0xBF
Invoke-RestMethod 'https://gist.githubusercontent.com/maskati/69324c8232cc93914b65b8b300a60dc5/raw/a082827c9cb6faacd9f89a5c803d91daef3d1b3e/bom.xml'

# fails because XmlDocument.LoadXml cannot handle initial BOM character
[xml](Invoke-RestMethod 'https://gist.githubusercontent.com/maskati/69324c8232cc93914b65b8b300a60dc5/raw/a082827c9cb6faacd9f89a5c803d91daef3d1b3e/bom.xml')

# ok, since initial BOM character removed
[xml]((irm 'https://gist.githubusercontent.com/maskati/69324c8232cc93914b65b8b300a60dc5/raw/a082827c9cb6faacd9f89a5c803d91daef3d1b3e/bom.xml') -replace '^[\uFEFF]')

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment