Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting XML Parse error when run below query in SQL 2005

Posted on 2009-07-03
5
Medium Priority
?
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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