You should look at For Xml Explicit. For example, so using the following schema:
Create Table #Employees
(
Id int not null identity(1,1) primary key clustered
, EmployeeName nvarchar(100) not null
, EmployeeSalary decimal(18,4) not null
)
Insert #Employees Values( 'ABC', 12345 )
Insert #Employees Values( 'DEF', 67890 )
Insert #Employees Values( 'GHI', 11223 )
And the following query:
Select 1 As Tag
, Null As Parent
, Id As [EmployeeDetails!1!Id!Hide]
, Null As [EmployeeName!2!Name]
, Null As [EmployeeSalary!3!Salary]
From #Employees
Union All
Select 2 As Tag
, 1 As Parent
, Id
, EmployeeName
, Null
From #Employees
Union All
Select 3 As Tag
, 1 As Parent
, Id
, Null
, EmployeeSalary
From #Employees As EmployeeDetails
Order By [EmployeeDetails!1!Id!Hide]
For Xml Explicit
I get the following which is close:
<EmployeeDetails>
<EmployeeName Name="ABC"/>
<EmployeeSalary Salary="12345.0000"/>
</EmployeeDetails>
<EmployeeDetails>
<EmployeeName Name="DEF"/>
<EmployeeSalary Salary="67890.0000"/>
</EmployeeDetails>
<EmployeeDetails>
<EmployeeName Name="GHI"/>
<EmployeeSalary Salary="11223.0000"/>
</EmployeeDetails>