im trying to generate xml in the following format:
<Root>
<Domain>Abc</Domain>
<Name>Xyz</Name>
<Contents>
<Content>
<ID>1</ID>
<Value>Test 1</Value>
<Record ID="1">Test 1</Record>
</Content>
<Content>
<ID>2</ID>
<Value>Test 2</Value>
<Record ID="2">Test 2</Record>
</Content>
</Contents>
</Root>
My query is as follows:
declare @TestTable table (ID int, Value varchar(100))
insert into @TestTable values (1,'Test 1')
insert into @TestTable values (2,'Test 2')
declare @Domain varchar(max)='Abc'
declare @Name varchar(max)='Xyz'
SELECT
1 AS Tag,
NULL AS Parent,
@Domain as 'Root!1!Domain!Element',
@Name as 'Root!1!Name!Element',
NULL as 'Contents!2!Element',
NULL as 'Content!3!ID!Element',
NULL as 'Content!3!Value!Element',
NULL as 'Content!3!Record!Element'
union
SELECT
2 AS Tag,
1 AS Parent,
NULL,NULL,NULL,NULL,NULL,NULL
union
select
3 as Tag,
2 as Parent,
NUll,NUll,NULL,
ID,Value,Value
from @TestTable
FOR XML EXPLICIT
my query does not produce the record tag completely, currently it is
<Record>Test 2</Record>
which should be as
<Record ID=2>Test 2</Record>
I tried all the possibilities but not getting the tag. Can anyone help me solving this issue.