5

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. 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
XML parsing: line 1, character 13, A string literal was expected

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?

2 Answers 2

11

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).

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I too have noticed that it seems much easier to parse XML than it is to parse the XML specification. ;-)
3

Attributes, ID is an attribute, must be quoted. Try ID="99"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.