Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error When converting Nvarchar to XML Expand / Collapse
Author
Message
Posted Tuesday, November 6, 2012 4:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 24, 2014 5:47 AM
Points: 62, Visits: 224
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
Post #1381481
Posted Tuesday, November 6, 2012 4:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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
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

Post #1381482
Posted Tuesday, November 6, 2012 4:24 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 24, 2014 5:47 AM
Points: 62, Visits: 224
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>
Post #1381486
Posted Tuesday, November 6, 2012 4:35 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:58 PM
Points: 1,683, Visits: 19,607
Your end tag is wrong

'/<Data Extract>'

should be

'</Data Extract>'


____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1381488
Posted Tuesday, November 6, 2012 4:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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
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

Post #1381490
Posted Tuesday, November 6, 2012 5:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 24, 2014 5:47 AM
Points: 62, Visits: 224

Thanks so much, I removed the spaces and corrected the syntax. It works perfectly.
Post #1381509
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse