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

25,546

Solution 1

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.

Solution 2

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

Share:
25,546
Christian
Author by

Christian

Updated on November 05, 2020

Comments

  • Christian
    Christian over 3 years

    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?