'How to make VSCode use declared encoding within XML files?

Context

XML files very likely declare their used character encoding, which is not properly taken into account by VScode compared to other editors. While in the past there was very limited automatic encoding detection, things have changed in the meantime and VScode tries to detect encoding of text files when users e.g. select a different encoding. Though, from my experience, the made suggestions for XML files are very likely wrong as well, while things could be pretty safe when looking at the file itself.

The following is an example of some XML file encoded using windows-1252 and properly declaring that. VScode opens the file using UTF-8 by default, most likely because no workspace config is available, and suggests it being windows-1255.

image

Missing alternatives in VSCode

Handling XML files properly is especially important because VSCode doesn't support charset of EditorConfig yet, which would otherwise allow setting different encodings for different file names. Additionally, the support for file extension specific settings of VScode is limited as well, preventing defining multiple different XML files names or even paths with different, but known encodings.

I e.g. have a lot of legacy projects using XML files for configs and depending on the age of those projects, many of them use different encodings. Additionally, some of the software even doesn't use a somewhat recent XML parser or a parser at all and is therefore limited to really only support windows-1252.

Feature request(s) to vote one

Would be great if VScode would be better in handling those XML files, so a feature request/issue has been created (again). People of interest need to vote on this issue to even get a chance to see a better implementation at some point.

What to do without VSCode fixing this?

Other than those issues getting created and closed over and over again, how can this be handled better with existing VSCode? Is there any mechanism I've missed currently, possibly some extension taking care already?

The only chance I see is using the same encoding for all XML files in some project, so that that encoding can be configured in .vscode/settings.json or is the global fallback default encoding already. But as said, that might not be possible for some legacy codebase.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source