Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error When converting Nvarchar to XML


Error When converting Nvarchar to XML

Author
Message
Teee-SQL
Teee-SQL
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 229
Hi all,

I'm trying to convert a string to XML, I'm not sure why I'm getting the error:XML parsing: line 1, character 14, equal expected
When I run the stored proc below to select the XML data. Please help

Kind Regards
Teee

CREATE PROCEDURE Test

AS
BEGIN

   DECLARE @sql Nvarchar(max)

   SET @sql = '<Data Extract>'
   SET @sql = @sql + (SELECT Top 2 FirstName, LastName
                      FROM
                     [dbo].[CandPersonal] pe
                     INNER JOIN CandProfile pr on pe.CandId = pr.Id
                     WHERE pr.isGraduateApplicant = '1'
                     FOR XML PATH('Candidate'), ROOT('Graduates'))
   SET @sql = @sql + (SELECT Top 2 FirstName, LastName
                      FROM
                     [dbo].[CandPersonal] pe
                     INNER JOIN CandProfile pr on pe.CandId = pr.Id
                     WHERE pr.isGraduateApplicant = '0'
                     FOR XML PATH('Candidate'), ROOT('ExperiencedHires'))
   SET @sql = @sql + '/<Data Extract>'
   


   SELECT CAST(@sql as XML) --XML parsing: line 1, character 14, equal expected
END
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
What is the output of @SQL before you convert it to XML?


DECLARE @sql Nvarchar(max)

SET @sql = '<Data Extract>'
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '1'
FOR XML PATH('Candidate'), ROOT('Graduates'))
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '0'
FOR XML PATH('Candidate'), ROOT('ExperiencedHires'))
SET @sql = @sql + '/<Data Extract>'

SELECT @SQL





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Teee-SQL
Teee-SQL
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 229
here's the output:
<Data Extract><Graduates><Candidate><FirstName>Shaun</FirstName><LastName>Slabber</LastName></Candidate><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate></Graduates><ExperiencedHires><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate><Candidate><FirstName>Meera</FirstName><LastName>Bhana</LastName></Candidate></ExperiencedHires>/<Data Extract>
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 22778
Your end tag is wrong

'/<Data Extract>'

should be

'</Data Extract>'

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6091 Visits: 6069
Also you cannot have spaces in element names, <Data Extract> needs to be <DataExtract>


DECLARE @SQL NVARCHAR(MAX) = '<DataExtract><Graduates><Candidate><FirstName>Shaun</FirstName><LastName>Slabber</LastName></Candidate><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate></Graduates><ExperiencedHires><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate><Candidate><FirstName>Meera</FirstName><LastName>Bhana</LastName></Candidate></ExperiencedHires></DataExtract>'
select @SQL
select CAST(@sql as xml)





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Teee-SQL
Teee-SQL
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 229
Thanks so much, I removed the spaces and corrected the syntax. It works perfectly.
:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search