XML解析:第1行,第23个字符,非法名称字符

时间:2013-08-28 06:57:12

标签: xml sql-server-2008

有人知道为什么这个XML文本会在SQL Server 2008中抛出非法的名称字符错误吗?

'<cs><c a="2" b="CITY & STATE TX" c="CITY & STATE TX"/></cs>'

异常消息是

Msg 9421, Level 16, State 1, Line 2
XML parsing: line 1, character 23, illegal name character

以下是用于解析此xml的查询

DECLARE @CaptionsDescriptions XML = '<cs><c a="2" b="CITY & STATE TX" c="CITY & STATE TX"/></cs>'

DECLARE @DocHandle int  
DECLARE @CaptionsDescriptionsTable TABLE  
(  
 ID INT IDENTITY(1,1),  
 languageID INT,  
 Caption  VARCHAR(50),  
 Description VARCHAR(2000)  
)  

EXEC sp_xml_preparedocument @DocHandle OUTPUT,@CaptionsDescriptions    
INSERT INTO @CaptionsDescriptionsTable SELECT a,b,c  
 FROM OPENXML(@DocHandle,'cs/c')   
 WITH (  
    a int, -- language id  
    b varchar(50), -- caption  
    c varchar(2000) -- description  
   )  

-- remove document handler  
EXEC sp_xml_removedocument @DocHandle  

3 个答案:

答案 0 :(得分:17)

&是XML中的保留/特殊字符。它应该是&amp;

我甚至会补充说XML parsing: line 1, character 23, illegal name character非常清楚。如果您认为SQL从1开始计数,则第23个字符是&

答案 1 :(得分:4)

您需要将&amp; 更改为&amp;

请阅读this

答案 2 :(得分:0)

将您的值置于cdata封面下,如此

DECLARE @CaptionsDescriptions XML = '<cs><c a="2" b="<![CDATA[CITY & STATE TX]>" c="CITY & STATE TX"/></cs>'