Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

XML Parsing - Unable to switch the encoding - Error Expand / Collapse
Author
Message
Posted Monday, July 5, 2010 9:01 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 5:50 PM
Points: 257, 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?




Post #947683
Posted Tuesday, July 6, 2010 1:33 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 12:23 AM
Points: 2,397, Visits: 3,411
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"
Post #947742
Posted Wednesday, July 7, 2010 6:40 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, October 23, 2013 5:50 PM
Points: 257, 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.
Post #948928
Posted Friday, March 15, 2013 9:01 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, September 25, 2014 6:04 PM
Points: 408, Visits: 970
thanks for the unicode reminder.... N'Was Scratching my head too'
Post #1431577
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse