XML Parsing - Unable to switch the encoding - Error

  • Hi,

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

    declare @xml xml

    set @xml =

    '[Code]<?xml version="1.0" encoding="utf-16"?>

    <root>

    <names>

    <name>test</name>

    </names>

    <names>

    <name>test1</name>

    </names>

    </root>

    [/Code]

    '

    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?

  • 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"

  • 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.

  • thanks for the unicode reminder.... N'Was Scratching my head too'

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply