I have some weird requirements that demand a specific XML format. Due to short deadlines and my lack of skills I decided to do a fast solution where I generate the XML through string concatenation.
DECLARE @tbl TABLE(personID int identity, name varchar(20), lastname varchar(20), country varchar(20));
INSERT INTO @tbl VALUES
('bob','bobby','USA')
,('mike','mikeson','Canada')
,('jack', 'jackson', 'Mexico')
select '<personID="' + cast(personID as varchar) +'" country="' + country + '"/>' +
'"<FIELD fieldname="name" value="' + name + '"/>' +
'<FIELD fieldname="lastname" value="' + lastname + '"/>' +
'</personID>'
from @tbl
and this gives the output which I need. However, I've been told numerous times that this is not best practice, and creating XMLs through string concatenation is discouraged. Is there some other way I can achieve the same outcome using more advanced XML techniques?
<personID="abc"confuses an element and an attribute.