简体   繁体   English

XML解析错误:在表中插入值时,“需要字符串文字”

[英]XML parsing error: 'A string literal was expected' when inserting values into table

In Microsoft SQL Server 2008 I am attempting to insert values into an sql table with the columns type_id of datatype int and xml_info of datatype XML . 在Microsoft SQL Server 2008中,我试图将值插入数据类型为int的type_id和数据 类型为XML的xml_info的值的sql表中。 I am using the following query: 我正在使用以下查询:

INSERT INTO tbl_applied_devices ([type_id],[xml_info])
VALUES (1,'<Profile ID=99><Server><ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>  <Name>localhost</Name><Services></Services></Server></Profile>')

But I keep getting this error: 但我不断收到此错误:

Msg 9413, Level 16, State 1, Line 4 讯息9413,第16级,州1,第4行
XML parsing: line 1, character 13, A string literal was expected XML解析:第1行,字符13,应为字符串文字

What am I doing incorrectly? 我做错了什么?

EDIT: I found that the source of the error is the xml element's ID attribute, <Profile ID=99> seems to be what is causing the error. 编辑:我发现错误的根源是xml元素的ID属性, <Profile ID=99>似乎是导致错误的原因。 How can I correctly insert xml with an attribute ? 如何正确插入带有属性的xml? Do I need to somehow escape one of the characters? 我需要以某种方式逃脱其中一个角色吗?

XML documents are text ual by nature. XML文档本质上是文本的。 This is not HTML for a web page, so you need ALL attribute values in quotes (single or double). 这不是网页的HTML,因此您需要在引号中包含所有属性值(单引号或双引号)。

INSERT INTO tbl_applied_devices ([type_id],[xml_info])
VALUES (1,'<Profile ID="99"><Server><ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>  <Name>localhost</Name><Services></Services></Server></Profile>')

It's very hard to parse the XML 1.0 specifications but here's a Microsoft version for .Net 4.5 , which is just as relevant for SQL Server XML. 解析XML 1.0规范非常困难,但这是.Net 4.5的Microsoft版本,与SQL Server XML同样重要。

Attributes, ID is an attribute, must be quoted. 属性(ID是属性)必须加引号。 Try ID="99" 尝试ID =“ 99”

MSSQL,PDO,插入 XML 字符串时出错:一般错误:102 ' 附近的语法不正确<!--?xml version=“1.0”</div--><div id="text_translate"><p> 我正在尝试将 XML 字符串从 PHP 插入 MSSQL 数据库。 我得到:</p><blockquote><p> SQLSTATE[HY000]:一般错误:102 ' 附近的语法不正确(后面是 xml 的块)。</p></blockquote><p> 准备好的语句是:</p><pre> $stmt = $this-&gt;db-&gt;prepare(' INSERT INTO [System_XmlExchangeImport] ([Create_ID], [ImportCode], [InputXml]) VALUES (?, ?, ?); '); $result = $stmt-&gt;execute(array('ABCDEFGHKAJSDKLJ', $code, $xml));</pre><p> $xml是一个有效的 XML 字符串。 可以使用 SQL 客户端应用程序(sqlectron)中的相同语句插入相同的 XML</p><p> 我不知道下一步该做什么。</p></div> - MSSQL, PDO, error when inserting XML string: General error: 102 Incorrect syntax near '<?xml version=“1.0”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在表中插入值时出错 - Error inserting values in a table 将 XML 插入 Oracle 中的 XMLTYPE 列时,字符串文字太长 - String literal too long while inserting a XML into XMLTYPE Column in Oracle 使用XML向表中插入多个值 - Inserting multiple values to a table using XML MSSQL,PDO,插入 XML 字符串时出错:一般错误:102 ' 附近的语法不正确<!--?xml version=“1.0”</div--><div id="text_translate"><p> 我正在尝试将 XML 字符串从 PHP 插入 MSSQL 数据库。 我得到:</p><blockquote><p> SQLSTATE[HY000]:一般错误:102 ' 附近的语法不正确(后面是 xml 的块)。</p></blockquote><p> 准备好的语句是:</p><pre> $stmt = $this-&gt;db-&gt;prepare(' INSERT INTO [System_XmlExchangeImport] ([Create_ID], [ImportCode], [InputXml]) VALUES (?, ?, ?); '); $result = $stmt-&gt;execute(array('ABCDEFGHKAJSDKLJ', $code, $xml));</pre><p> $xml是一个有效的 XML 字符串。 可以使用 SQL 客户端应用程序(sqlectron)中的相同语句插入相同的 XML</p><p> 我不知道下一步该做什么。</p></div> - MSSQL, PDO, error when inserting XML string: General error: 102 Incorrect syntax near '<?xml version=“1.0” 将XML插入SQL表并进行解析后,得到“ ???”而不是文本 - After inserting XML into SQL table and parsing it getting “???” instead of text 在函数内的表中插入值会收到错误,但是当尝试在函数外的表中插入值时,它可以正常工作 - Inserting values into a table within a function receives an error, but when trying to insert values into the table outside of a function it works fine 从表到表插入值时出错 - Error while inserting values from table to table 将空值作为字符串插入sql表 - Inserting empty values as string to sql table XQuery [value()]:期望字符串文字 - XQuery [value()]: A string literal was expected 在表中插入值时出错? - getting error while inserting values into table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM