当将字符串转换为SQL Server XML数据类型时,为什么回车符会消失?

3
每当我将一个字符串解析为SQL XML数据类型时,我注意到当混合使用空格和XML回车符时,回车符会消失。如果它们没有混合使用,则一切正常。有人知道这是为什么吗?
请参见下面的SQL示例代码。
DECLARE @var XML

PRINT 'This is a statement with two XML carriage-return characters'
SET @var = CONVERT (XML,'<root>Dear Sir,&#xD;&#xD;I am passing CR</root>',1)
SELECT @var

PRINT 'output is identical to a statement with two whitespace carriage-return characters'
SET @var = CONVERT (XML,'<root>Dear Sir,

I am passing CR</root>',1)
SELECT @var

PRINT 'Why does this statement only display one space? There is an XML carriage-return AND a whitespace carriage-return character.' 

--Make sure there is no space after &#xD; after you've copied and pasted the code 
SET @var = CONVERT (XML,'<root>Dear Sir,&#xD;
I am passing CR</root>',1)
SELECT @var
1个回答

2
在Windows中,行末是CR-LF。在Unix中,行末是CR。
在第一个例子中,你使用了CR-CR。我猜SQL Server将其解释为Unix风格的行尾,给出两个空格。
你的第二个例子是在Windows中输入的,给出CR-LF-CR-LF。这被解释为Windows风格的行尾,给出两个空格。
你的第三个例子是CR-CR-LF。显然,这被解释为Windows风格的行尾,给出一个空格和一个不匹配的CR。
如果你将第三个例子更改为使用CR-LF或&#xD;&#xA;,它将显示两个空格。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接