SQL XML 解析错误:将值插入表时,“需要一个字符串文字”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13223781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML parsing error: 'A string literal was expected' when inserting values into table
提问by Christian
In Microsoft SQL Server 2008 I am attempting to insert values into an sql table with the columns type_id of datatype intand xml_info of datatype XML. I am using the following query:
在Microsoft SQL Server 2008中,我试图插入值到一个SQL表中的列TYPE_ID数据类型为int和数据类型的XML xml_info。我正在使用以下查询:
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
XML parsing: line 1, character 13, A string literal was expected
消息 9413,级别 16,状态 1,第 4 行
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. How can I correctly insert xml with an attribute ? Do I need to somehow escape one of the characters?
编辑:我发现错误的来源是 xml 元素的 ID 属性,<Profile ID=99>
似乎是导致错误的原因。如何正确插入带有属性的 xml?我需要以某种方式逃避其中一个角色吗?
回答by RichardTheKiwi
XML documents are textual by nature. This is not HTML for a web page, so you need ALL attribute values in quotes (single or double).
XML 文档本质上是文本文本。这不是网页的 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 specificationsbut 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 一样相关。
回答by Duccio Fabbri
Attributes, ID is an attribute, must be quoted. Try ID="99"
属性,ID是一个属性,必须加引号。试试 ID="99"