“<! - ?xml version =”1.0“encoding =”UTF-16“standalone =”no“? - >”在XML开始时“无法切换编码”错误

时间:2017-05-18 14:00:43

标签: sql-server xml-parsing

以这种方式粉碎SQL中的XML:

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)

它运行正常,但是当XML是

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)

我收到以下问题“无法切换编码”

为什么呢?请帮忙

1 个答案:

答案 0 :(得分:2)

因为你的字符串不在utf-16中。在字符串文字之前添加N以使其成为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)