“unable to switch the encoding” error when “<?xml version=”1.0“ encoding=”UTF-16“ standalone=”no“ ?>” at XML beginning

While shredding the XML in SQL this way:

DECLARE @XML2 XML
SET @XML2 = '
<App_MemberServers>
<memberServers>
<client _type_="3" clientId="3" clientName="TokyoProxy" clientSidePackage="1" consumeLicense="1" srmReportSet="0" srmReportType="0" type="0"/>
</memberServers>
</App_MemberServers>'

select a.b.value('@clientId','INT') ClientID
from @XML2.nodes('/App_MemberServers/memberServers/client') a(b)

It works fine, but when the XML is

  • SQL Server Full Text Search - Is it possible to search in the middle of a word?
  • SQLSRV Not Showing in PHP INFO
  • Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'D:'
  • How do you comment your db schema objects in SQL Azure database project?
  • Maximum recursion has been exhausted
  • Microsoft Sync Framework - Performance and scalability
  • DECLARE @XML2 XML
    SET @XML2 = '<?xml version="1.0" encoding="UTF-16" standalone="no" ?>
    <App_MemberServers>
    <memberServers>
    <client _type_="3" clientId="3" clientName="TokyoProxy" clientSidePackage="1" consumeLicense="1" srmReportSet="0" srmReportType="0" type="0"/>
    </memberServers>
    </App_MemberServers>'
    
    select a.b.value('@clientId','INT') ClientID
    from @XML2.nodes('/App_MemberServers/memberServers/client') a(b)
    

    I get the following issue “unable to switch the encoding”

    Why? Please help

  • Mysql to get the current date with time 23:59:59
  • How to ingore a .sql file on build but include it on schema compare
  • Scheduling tasks Advice? .Net, SQL Job?
  • Help me to create a join based query in SQL Server 2008
  • Configurable Serial Tracking Number Generation
  • SQL Server Function To Check String Based on Format and Convert to Date
  • One Solution collect form web for ““unable to switch the encoding” error when “<?xml version=”1.0“ encoding=”UTF-16“ standalone=”no“ ?>” at XML beginning”

    because your string is not in utf-16. add N before string literal to make it UTF:

    DECLARE @XML2 XML
    SET @XML2 = N'<?xml version="1.0" encoding="UTF-16" standalone="no" ?>
    <App_MemberServers>
    <memberServers>
    <client _type_="3" clientId="3" clientName="TokyoProxy" clientSidePackage="1" consumeLicense="1" srmReportSet="0" srmReportType="0" type="0"/>
    </memberServers>
    </App_MemberServers>'
    
    select a.b.value('@clientId','INT') ClientID
    from @XML2.nodes('/App_MemberServers/memberServers/client') a(b)
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.