[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-11
9
Medium Priority
?
8,666 Views
Last Modified: 2008-09-17
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?
0
Comment
Question by:jkelly061597
9 Comments
 

Author Comment

by:jkelly061597
ID: 12561075
Alternatively, I can remove the '<?xml version="1.0" encoding="utf-8" ?>' from the XML and it works fine as well.
0
 
LVL 13

Expert Comment

by:dungla
ID: 12561873
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
0
 

Author Comment

by:jkelly061597
ID: 12561999
That is actually the problem. When I use the nVarChar I get the error. When I use varchar I do not get the error.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:vascov
ID: 12562554
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
0
 

Author Comment

by:jkelly061597
ID: 12567313
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.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 750 total points
ID: 12567446
As has been pointed out:

If encoding = 'utf-8'
    use varchar
Else (if encoding = 'utf-16')
    use nvarchar
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 12567502
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.
0
 

Author Comment

by:jkelly061597
ID: 12570818
Thanks everyone,

I thought utf8 & 16 should both be stored in n*.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 12570989
>>I thought utf8 & 16 should both be stored in n*.<<
But then life would be too easy <g>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question