Вашему формату потребуется довольно много вложенных уровней.Попробуйте это так (и в следующий раз, пожалуйста, предоставьте образцы данных в формате расходных материалов, как я делаю это здесь с DDL и INSERT):
DECLARE @AdminUsers TABLE(UserName VARCHAR(100),Computer VARCHAR(100));
DECLARE @NormalUsers TABLE(UserName VARCHAR(100),Computer VARCHAR(100));
INSERT INTO @AdminUsers VALUES('Admin1','blah')
,('Admin2','blah')
,('Admin3','Other');
INSERT INTO @NormalUsers VALUES('user1','blah')
,('user2','blah')
,('user3','Other');
DECLARE @ComputerName VARCHAR(100)='blah';
SELECT 'true' AS [Device/@AppEnabled]
,GETDATE() AS [Device/@AsOf]
,@ComputerName AS [Device/@MachineName]
,(
SELECT
(
SELECT 'Administrators' AS [@Name]
,'true' AS [@ReportChanges]
,(
SELECT UserName AS [Member/@name]
FROM @AdminUsers
WHERE Computer=@ComputerName
FOR XML PATH(''),ROOT('Members'),TYPE
) AS [*]
FOR XML PATH('Group'),TYPE
) AS [*]
,(
SELECT 'Normal Users' AS [@Name]
,'true' AS [@ReportChanges]
,(
SELECT UserName AS [Member/@name]
FROM @NormalUsers
WHERE Computer=@ComputerName
FOR XML PATH(''),ROOT('Members'),TYPE
) AS [*]
FOR XML PATH('Group'),TYPE
) AS [*]
FOR XML PATH(''),TYPE
) AS [Device/Groups]
FOR XML PATH('Application'),TYPE;
Результат
<Application>
<Device AppEnabled="true" AsOf="2019-06-04T10:10:50.160" MachineName="blah">
<Groups>
<Group Name="Administrators" ReportChanges="true">
<Members>
<Member name="Admin1" />
<Member name="Admin2" />
</Members>
</Group>
<Group Name="Normal Users" ReportChanges="true">
<Members>
<Member name="user1" />
<Member name="user2" />
</Members>
</Group>
</Groups>
</Device>
</Application>
ОБНОВЛЕНИЕ
Другой подход с тем же результатом
WITH Groups AS
(
SELECT 'Administrators' AS GroupName
UNION ALL
SELECT 'Normal Users'
)
SELECT 'true' AS [@AppEnabled]
,GETDATE() AS [@AsOf]
,@ComputerName AS [@MachineName]
,(
SELECT GroupName AS [@name]
,'true' AS [@ReportChanges]
,(
SELECT names.*
FROM
(
SELECT UserName AS [@Name] FROM @AdminUsers WHERE GroupName='Administrators' AND Computer=@ComputerName
UNION ALL
SELECT UserName AS [@Name] FROM @NormalUsers WHERE GroupName='Normal Users' AND Computer=@ComputerName
) names
FOR XML PATH('Member'),TYPE
) AS Members
FROM Groups
FOR XML PATH('Group'),TYPE
) AS Groups
FOR XML PATH('Device'),ROOT('Application');