XML parsing, illegal character in the end of the string

自闭症网瘾萝莉.ら 提交于 2019-12-11 20:28:42

问题


I'm getting very strange error when I trying to convert a string to XML in MS SQL Server:

Msg 9420, Level 16, State 1, Line 5
XML parsing: line 1, character 8071, illegal xml character

If I check the string in some text editor, I can see that its length is 8070. Why is it complaining about character 8071 if it does not exist?

This is how I'm converting string to XML:

CAST(REPLACE(SUBSTRING(
REPLACE(REPLACE(REPLACE(ResponseData,'ä','a'),'ö','o'),'å','a'), 
PATINDEX('%<?xml%',ResponseData), PATINDEX('%sonType>', ResponseData)+6),
'<?xml version="1.0" encoding="utf-16"?>', 
'<?xml version="1.0" encoding="utf-8"?>')as XML) as ResponseData

Are any of replaces causing the problem?

UPD: The problem also is that in ResponseData column the XML string is stored together with some other data. Example:

Error from service: <Some error description>. Sent request: <?xml version="1.0" encoding="utf-16"?><Contents of the XML>

So I need to get that XML string from the column and then convert it to XML.


回答1:


You could try to change original encoding from UTF-16 to ISO-8859-1, or a more precise encoding for your characters:

DECLARE @data varchar(max) = '<?xml version="1.0" encoding="utf-16"?><...>' 
SELECT CAST(REPLACE(@data, 
            '<?xml version="1.0" encoding="utf-16"?>',
            '<?xml version="1.0" encoding="iso-8859-1"?>') AS XML) ResponseData


来源:https://stackoverflow.com/questions/20116205/xml-parsing-illegal-character-in-the-end-of-the-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!