XML解析:第1行,第80个字符,意外的输入结束

时间:2015-08-06 09:50:34

标签: sql sql-server xml

尝试运行此操作时,我收到上述错误。我已经确定了问题 - 因为 V& amp; ;在描述中,但我不知道如何解决它。我无法使用替换,因为我可能会再次使用不同的字符/单词(例如 with& amp )。谢谢

declare @x xml
set @x = '<options>
<option>
<code>95544</code>
<description>17&amp;#34;&amp;#32;Star&amp;#32;spoke&amp;#32;alloy&amp;#32;wheels&amp;#32;&amp;#45;&amp;#32;style&amp;#32;393</description>
<monthlycost>0.00</monthlycost>
<allowed />
</option>
<option>
<code>107394</code>
<description>19&amp;#34;&amp;#32;Individual&amp;#32;V&amp;#32;spoke&amp;#32;alloy&amp;#32;wheels&amp;#32;&amp;#45;&amp;#32;Style&amp;#32;626I</description>
<monthlycost>37.13</monthlycost>
<allowed />
</option>
<option>
<code>86469</code>
<description>Adaptive&amp;#32;M&amp;#32;Sports&amp;#32;suspension</description>
<monthlycost>21.09</monthlycost>
<allowed />
</option>
<option>
<code>61202</code>
<description>Metallic&amp;#32;&amp;#45;&amp;#32;Black&amp;#32;sapphire</description>
<monthlycost>8.15</monthlycost>
<allowed />
</option>
<option>
<code>94722</code>
<description>Move&amp;#32;cloth&amp;#32;&amp;#45;&amp;#32;Anthracite</description>
<monthlycost>0.00</monthlycost>
<allowed />
</option>
<option>
<code>11646</code>
<description>Solid&amp;#32;&amp;#45;&amp;#32;Alpine&amp;#32;white</description>
<monthlycost>0.00</monthlycost>
<allowed />
</option>
</options>'


select 
pref.value('(code/text())[1]', 'varchar(32)') as Code           
,pref.value('(description/text())[1]', 'varchar(80)') as [Description]
,CONVERT(XML, 
        pref.value('(description/text())[1]', 'varchar(80)')
 ).value('.', 'varchar(80)') as [DescriptionFixed]
 ,pref.value('(monthlycost/text())[1]', 'varchar(32)') as MontlyCost  
from 
@X.nodes('/options/option') AS Options(pref)

1 个答案:

答案 0 :(得分:7)

问题是您的XML字符串被截断,留下了不完整的实体/无效字符(请注意尾随&):

19&#34;&#32;Individual&#32;V&#32;spoke&#32;alloy&#32;wheels&#32;&#45;&#32;Style&

您只需增加varchar大小即可解决问题,例如:

,CONVERT(XML, 
        pref.value('(description/text())[1]', 'varchar(max)')
 ).value('.', 'varchar(max)') as [DescriptionFixed]