SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Parsing - Unable to switch the encoding - Error


XML Parsing - Unable to switch the encoding - Error

Author
Message
Aparna-1
Aparna-1
Old Hand
Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)

Group: General Forum Members
Points: 325 Visits: 601
Hi,

I am getting the following error when I try to parse XML.


declare @xml xml
set @xml =
'
<?xml version="1.0" encoding="utf-16"?>
<root>
<names>
<name>test</name>
</names>
<names>
<name>test1</name>
</names>
</root>


'

SELECT
x.value('name[1]', 'varchar(10)') as Name
FROM
@xml.nodes('/root/names') t(x)

This XML is passed from a C# program.
This seems to work fine if i change the encoding from utf-16 to utf-8.
Why does the XM L parser fail with utf-16 as the default encoding?
SwePeso
SwePeso
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4101 Visits: 3433
Works for me. Remember UTF-16 is similar to UNICODE.
Please note the N string-prefix.

declare @xml xml
set @xml =
N'<?xml version="1.0" encoding="utf-16"?>
<root>
<names>
<name>test</name>
</names>
<names>
<name>test1</name>
</names>
</root>

'

SELECT
x.value('name[1]', 'varchar(10)') as Name
FROM
@xml.nodes('/root/names') t(x)




N 56°04'39.16"
E 12°55'05.25"
Aparna-1
Aparna-1
Old Hand
Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)

Group: General Forum Members
Points: 325 Visits: 601
Works for me now. Thanks a lot.
I tried parsing it as nvarchar instead of varchar, but somehow missed to spot N while passing the parameters.
Tom Van Harpen
Tom Van Harpen
SSC Eights!
SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)SSC Eights! (900 reputation)

Group: General Forum Members
Points: 900 Visits: 1102
thanks for the unicode reminder.... N'Was Scratching my head too'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search