Solved

Getting XML Parse error when run below query in SQL 2005

Posted on 2009-07-03
5
138 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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