将值传递给sp_xml_preparedocument时获取错误

时间:2017-04-25 09:42:33

标签: sql-server xml

DECLARE @xml XML;

SET @xml = N'"<?xml version=\"1.0\" encoding=\"utf-16\"?><NominalIds><Id>6416</Id><Id>9</Id><Id>6405</Id><Id>6400</Id><Id>6404</Id><Id>6407</Id><Id>0</Id></NominalIds>"';
DECLARE @idoc INT

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

错误是

  

XML解析:第1行,第7个字符,text / xmldecl不在输入的开头

3 个答案:

答案 0 :(得分:1)

DECLARE @xml XML;

SET @xml = N'<?xml version=\"1.0\" encoding=\"utf-16\"?><NominalIds><Id>6416</Id><Id>9</Id><Id>6405</Id><Id>6400</Id><Id>6404</Id><Id>6407</Id><Id>0</Id></NominalIds>';
DECLARE @idoc INT

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

答案 1 :(得分:1)

您的xml字符串格式不正确

在开头和结尾删除双引号,在\

之前删除"
DECLARE @xml XML = N'<?xml version="1.0" encoding="utf-16"?><NominalIds><Id>6416</Id><Id>9</Id><Id>6405</Id><Id>6400</Id><Id>6404</Id><Id>6407</Id><Id>0</Id></NominalIds>';
DECLARE @idoc INT

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

--- do stuff

EXEC sp_xml_removedocument @idoc

答案 2 :(得分:0)

您已被告知,"\"错误了......

但是你应该走另一条路......

FROM OPENXML与相应的SP准备和删除文档已经过时,不应再使用(很少有例外)。而是使用适当的methods the XML data type provides

对于你的例子,这将是方法:

DECLARE @xml XML = 
N'<?xml version="1.0" encoding="utf-16"?><NominalIds><Id>6416</Id><Id>9</Id><Id>6405</Id><Id>6400</Id><Id>6404</Id><Id>6407</Id><Id>0</Id></NominalIds>';

SELECT id.value(N'(text())[1]',N'int')
FROM @xml.nodes(N'/NominalIds/Id') AS A(id)

结果

6416
9
6405
6400
6404
6407
0