I don't know enough about your setup to say precisely why, but this most often happens when you have an encoding specified in the XML document, but the data type that you are using causes xquery to think that it is a different encoding. You will see what I mean if you run the two queries belowDECLARE @x NVARCHAR(200) = '<?xml version="1.0" encoding="UTF-8"?> <a>123</a>';
SELECT CAST(@x AS XML).query('a');
DECLARE @x NVARCHAR(200) = '<?xml version="1.0" encoding="UTF-8"?> <a>123</a>';
SELECT CAST(CAST(@x AS VARCHAR(200)) AS XML).query('a');The first one gives you the error because the string specifies UTF-8 encoding, but the data type being NVARCHAR causes xquery to think that it is UTF-16. In the second query, where I am casting the string to VARCHAR before casting to XML, it works correctly.
Can you look through your code and/or set up to see if that type of thing is happening anywhere?