This is not a question on how to overcome the \"XML parsing: ... illegal xml character\" error, but about why it is happening? I know tha
Can you modify the XML encoding declaration? If so;
declare @xml XML = N'
';
select @xml
(No column name)
Both of these fail with illegal xml character:
set @xml = ' – '
set @xml = ' – '
because they pass a non-unicode varchar
to the XML parser; the string contains Unicode so must be treated as such, i.e. as an nvarchar
(utf-16) (otherwise the 3 bytes comprising the –
are misinterpreted as multiple characters and one or more is not in the acceptable range for XML)
This does pass a nvarchar
string to the parser,
but fails with unable to switch the encoding:
set @xml = N' – '
This is because an nvarchar
(utf-16) string is passed to the XML parser but the XML document states its utf-8 and the –
is not equivalent in the two encodings
This works as everything is utf-16
set @xml = N' – '