Link to home
Start Free TrialLog in
Avatar of jkelly061597
jkelly061597

asked on

XML parsing error: Switch from current encoding to specified encoding not supported.

I am using the XML capabilities of SQL Server 8 and am running the following:

-----------------------------------------------------------------------------
/** Begin Script for reproducing the problem **/

DECLARE @i_xml nVarChar(4000)

SELECT @i_xml = '<?xml version="1.0" encoding="utf-8" ?>
<TermRelations></TermRelations>'

declare @hdoc int

exec sp_xml_preparedocument @hdoc output,@i_xml

exec sp_xml_removedocument @hdoc
-----------------------------------------------------------------------------

I get the following response from Sql Server:
Server: Msg 6603, Level 16, State 1, Procedure sp_xml_preparedocument, Line 10
XML parsing error: Switch from current encoding to specified encoding not supported.


Now,

When I change @i_xml to the following:
DECLARE @i_xml VarChar(4000)

I do not get the error... What is going on here?

I will need to support arabic languages with this sproc, hence the use of nVarChar. Can anyone provide guidance?
Avatar of jkelly061597
jkelly061597

ASKER

Alternatively, I can remove the '<?xml version="1.0" encoding="utf-8" ?>' from the XML and it works fine as well.
jkelly,

VARCHAR is not for unicode charset (utf-8), if you want to parse the encoding utf-8 from XML to SQL server, use NVARCHAR because NVARCHAR is for unicode. That's all
That is actually the problem. When I use the nVarChar I get the error. When I use varchar I do not get the error.
Why are you specifying the enconding in the first place ? do you really need it ?
the encoding says something about how the data is supposed to be encoded, and changing from varchar to nvarchar definetely changes the encoding which is what the xml parser is complaining.
For arabic stuff just use nvarchar and do not specify any encoding.

hth

Vasco
The XML I am reading comes from another system, with the <xml> encoding string.

The actual SPROC uses an nText parameter to accept the xml, this demo uses the varchar/nvarchar due to availability of local variable types.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And the same applies to Text and nText.

I suspect you will have to come up with either two stored procedures or use two parameters (one text and the other ntext) and a flag to indicate encoding.  You could also change the encoding from utf-8 to utf-16, prior to passing to the stored procedure.
Thanks everyone,

I thought utf8 & 16 should both be stored in n*.
>>I thought utf8 & 16 should both be stored in n*.<<
But then life would be too easy <g>