[英]SQL Server: How to convert an XML variable to UTF-8 encoded varbinary(max)?
In SQL Server the default conversion from the xml type to varbinary uses XML text encoding (start and end tags with angled brackets etc) using UTF-16 character encoding. 在SQL Server中,从xml类型到varbinary的默认转换使用使用UTF-16字符编码的XML文本编码(带有尖括号的开始和结束标签等)。 Eg 例如
declare @xml xml = '<foo><bar>abc</bar></foo>';
declare @foo varbinary(max);
set @foo = CONVERT(varbinary(max), @xml, 2);
select @foo
Yields: 产量:
0xFFFE3C0066006F006F003E003C006200610072003E006100620063003C002F006200610072003E003C002F0066006F006F003E00 0xFFFE3C0066006F006F003E003C006200610072003E006100620063003C002F006200610072003E003C002F0066006F006F003E00
Notice the zeros, indicating the double byte characters. 注意零,表示双字节字符。 Is there a way to convert into UTF-8? 有没有办法转换为UTF-8?
Maybe it's not the best way to do this, but I solved this converting the xml to varchar
first: 也许这不是最好的方法,但是我首先解决了将xml转换为varchar
:
declare @xml xml = '<foo><bar>abc</bar></foo>';
declare @xmlvarchar varchar(max) = CONVERT(varchar(max), @xml)
declare @foo varbinary(max) = CONVERT(varbinary(max), @xmlvarchar);
select @foo
The output is: 输出为:
0x3C666F6F3E3C6261723E6162633C2F6261723E3C2F666F6F3E
Of course, varchar depends on your database collation, in my case (non special characters) this solution works perfectly. 当然,varchar取决于您的数据库排序规则,在我的情况下(非特殊字符),此解决方案可以完美地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.