DBAtlas · Database operations atlas

9421: XML parsing: illegal xml character

SQL Server

What does it mean?

SQL Server encountered a disallowed (invalid) character while parsing text as XML. The conversion to the XML type or the assignment to an XML column/variable failed because of this character.

Why does it happen?

Control characters forbidden in XML 1.0 (e.g. some characters in the 0x00-0x1F range), wrong encoding (encoding mismatch), or corrupt/unsanitized data from another source. Usually appears during CAST(... AS XML) or an INSERT into an XML column.

How to fix it

Strip invalid characters from the text before converting it to XML. Make the source encoding (UTF-8/UTF-16) compatible with the target; declare the correct encoding in the XML declaration. Remove control characters with REPLACE/NCHAR, or generate and validate the XML with a standard library in the application layer. If you must store the raw text, consider NVARCHAR(MAX).

Open in DBAtlas →

More from this engine