Error When converting Nvarchar to XML

  • 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

  • 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

  • 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>

  • 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
  • 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)

  • Thanks so much, I removed the spaces and corrected the syntax. It works perfectly.

    🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply