STATUS_XML_ENCODING_MISMATCH 0XC0150021 Fix
You see this when an XML file's declared encoding doesn't match the actual bytes. Real fix: align the declaration or re-save with the right encoding.
This error pops up when you try to load or parse an XML file—could be a config file, a feed, or an export from some old system. The exact trigger: the XML declaration says something like <?xml version="1.0" encoding="UTF-8"?>, but the file's actual bytes are in UTF-16, or vice versa. I've seen this most often with files generated by legacy Windows tools that write UTF-16 by default but slap a UTF-8 declaration in there. You'll see the error in Windows Event Viewer logs, or as a COM exception in .NET apps that try to parse XML.
Root cause in plain English
Every XML file can have an optional declaration at the top that tells parsers what encoding to expect. The parser trusts that declaration—until it reads the actual bytes and finds a mismatch. For example, UTF-16 files often start with a Byte Order Mark (BOM), two bytes like 0xFF 0xFE. If the declaration says UTF-8 but the BOM says UTF-16, the parser throws 0XC0150021. The fix is to make the declaration match reality, or change the file's encoding to match the declaration.
Fix it in 3 steps
Before you start, make a copy of the problematic XML file. You can break the original by saving it wrong, so keep a backup.
- Check the file's real encoding.
Open the file in a hex editor or Notepad++. If using Notepad++, look at the bottom-right corner—it shows the encoding (e.g., "UTF-8" or "UCS-2 LE BOM"). In a hex editor, the first two bytes tell the story:
FF FE= UTF-16 little-endian (UCS-2 LE)FE FF= UTF-16 big-endianEF BB BF= UTF-8 with BOM- No BOM at all = likely plain UTF-8 (but could be ANSI)
Write down the real encoding. Don't guess—verify it.
- Match the XML declaration to the real encoding.
Open the XML file in a text editor (Notepad works, but Notepad++ is better). Look at the very first line. If the real encoding is UTF-16 LE, change the declaration to:
<?xml version="1.0" encoding="UTF-16"?>If the real encoding is UTF-8 with BOM, use:
<?xml version="1.0" encoding="UTF-8"?>If the file has no BOM and is plain UTF-8, the declaration should be
encoding="UTF-8". Save the file.After saving, open the file again in Notepad++ and check the encoding indicator. It should now match the declaration. If it doesn't, move to step 3.
- Re-save the file with the correct encoding.
This is the nuclear option but it always works. Open the file in Notepad++. Go to Encoding menu > Encode in UTF-8 (or UTF-16, whichever you want). Then go to Encoding again > Convert to UTF-8 without BOM (or with BOM if the parser requires it). Save the file.
Now the declaration and the file bytes are in sync. After saving, the error should be gone.
What if it still fails?
Check for invisible characters at the start of the file. Some editors sneak in a BOM even when you don't see it. Use a hex editor to confirm the first two bytes match what you expect. Also check that the XML declaration is exactly on the first line—no blank lines before it. Some parsers are strict about that. If you're still stuck, open the file in a browser like Chrome; it will often report the exact line where the encoding breaks down. That line number can point you to the offending content.
Pro tip: If you're generating XML programmatically, always set the encoding explicitly. In C#, use
XmlWriterSettingswithEncoding.UTF8. In Python, uselxmlwithencoding='utf-8'. Don't rely on defaults—they vary by platform.
Was this solution helpful?