ope*_*sas 3 xml sql sql-server html-encode sql-server-2008
我正在解析一个 xml 文本,其中包含类似 \'\xc3\xa1 \xc3\xa9 \xc3\xb1\' 的字符。
\n\n我收到“在文本内容中发现无效字符。”错误,如下所示
\n\ndeclare @Xml varchar(100)\n\nset @Xml =\n\'\n<?xml version="1.0" encoding="UTF-8"?>\n<Root>\xc3\xa1</Root>\n\'\n\ndeclare @XmlId integer\n\nexecute dbo.sp_xml_preparedocument @XmlId output, @Xml\n\nselect * from openXml( @XmlId, \'/\', 2) with (\n Root varchar(10)\n)\nexecute dbo.sp_xml_removedocument @XmlId\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下错误:
\n\nThe XML parse error 0xc00ce508 occurred on line number 3, near the XML text "<Root>".\nMsg 6602, Level 16, State 2, Procedure sp_xml_preparedocument, Line 1\nThe error description is \'An invalid character was found in text content.\'.\nMsg 8179, Level 16, State 5, Line 13\nCould not find prepared statement with handle 0.\nMsg 6607, Level 16, State 3, Procedure sp_xml_removedocument, Line 1\nsp_xml_removedocument: The value supplied for parameter number 1 is invalid.\nRun Code Online (Sandbox Code Playgroud)\n\n有什么方法可以让sql解析这个xml吗?还是编码的问题?
\n\n是对这些字符进行编码的唯一解决方案还是有更优雅的方法来解决它?
\n今天我遇到了同样的错误,将序列化对象作为 xml 传递给我的存储过程。最终我找到了错误所在。
更改您的代码:
declare @Xml varchar(100)
Run Code Online (Sandbox Code Playgroud)
到:
declare @Xml nvarchar(100)
Run Code Online (Sandbox Code Playgroud)
这是我在网上找到的总结,希望对你有帮助。
列nvarchar可以存储任何 Unicode 数据。列varchar仅限于 8 位代码页(非 Unicode 字符数据)。使用nvarchar而不是varchar可以帮助您避免每次读取或写入数据库时都进行编码转换。