Solved

Getting XML Parse error when run below query in SQL 2005

Posted on 2009-07-03
5
136 Views
Last Modified: 2012-05-07
Getting XML Parse error when run below query:

DECLARE @XMLString XML,@newname varchar(5000)
SET @newname ='
<?xml version="1.0" encoding="utf-16"?>
<CustomFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IDValue>
    <ID>22</ID>
    <VALUE>Staging</VALUE>
  </IDValue>
</CustomFields>'


SELECT CAST(CAST ((@newname) AS VARBINARY (MAX)) AS XML)
0
Comment
Question by:ravinder_mask
  • 2
5 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24772492
try this one:




DECLARE @XMLString XML,@newname varchar(5000)
SET @newname ='
<CustomFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IDValue>
    <ID>22</ID>
    <VALUE>Staging</VALUE>
  </IDValue>
</CustomFields>'
 
SELECT CAST(CAST ((@newname) AS VARBINARY (MAX)) AS XML)

Open in new window

0
 

Author Comment

by:ravinder_mask
ID: 24811997
yes, it works but '<?xml version="1.0" encoding="utf-16"?>' this is generated by code at run time, I cannot remove it, so please suggest solution with this line. thanks is advance
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 25543776
I got this working:
§ utf-8 instead of utf-16
§ no spaces before the <?xml ... > stuff


DECLARE @XMLString XML,@newname varchar(5000)
SET @newname ='<?xml version="1.0" encoding="utf-8"?>
<CustomFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IDValue>
    <ID>22</ID>
    <VALUE>Staging</VALUE>
  </IDValue>
</CustomFields>'
 
set  @XMLString = @newname
 
SELECT CAST(@newname  AS XML)

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 25543782
aka:
DECLARE @XMLString XML,@newname varchar(5000)
SET @newname ='
<?xml version="1.0" encoding="utf-16"?>
<CustomFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IDValue>
    <ID>22</ID>
    <VALUE>Staging</VALUE>
  </IDValue>
</CustomFields>'
 
set  @XMLString = replace(ltrim(replace(replace(@newname, char(10), ''), char(13), '')), 'encoding="utf-16"' , 'encoding="utf-8"' )
select @xmlstring
-- SELECT CAST(@newname  AS XML)

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question