XML parsing unable to switch the encoding

When i tried to query a ntext column as xml with CAST function i had :

XML parsing: line 1, character 55, unable to switch the encoding

error. i did some research finally i succeed on it

Ex :

SELECT
colum1,
column2,
CAST(REPLACE(CAST(column3 AS NVARCHAR(MAX)),'utf-8','utf-16') AS XML)
FROM TABLE1

column3 : is ntext xml data which has
at the top of root element

1. Convert ntext to nvarchar for replace function (doesn't accept ntext datatype)
2. Replace 'utf-8' text with 'utf-16' because of SQL Server 2005 restrict
3.Cast result to XML and have fun :)





Comments

Matt B. said…
Thanks, very helpful! I just wanted to add that it doesn't work when there are ampersands in the XML…one small change fixes that:

CAST(REPLACE( REPLACE([column3],'utf-8','utf-16'), '&', '&') AS XML)
Steven Gomez said…
MUCHAS GRACIAS. Me ha servido de mucha ayuda.
THANKS A LOT. It has been of much help.

Popular posts from this blog

d3.js Introduction

HTML Vector Graphic PATH element