Declare @xmlDoc xml
SET @xmlDoc = (SELECT * FROM USERS AS UserTable For XML AUTO, ELEMENTS, ROOT('Root'))
SELECT @xmlDoc
Result as below :
<Root>
<UserTable>
<UserDBID>1</UserDBID>
<UserID>Admin </UserID>
<UserName>admin</UserName>
<Password>49CE5FB6D05148ACB66A</Password>
<FullName />
<LocalFullName />
<Title />
<Department />
<UserRole>Administrator</UserRole>
<IsFirstLogin>0</IsFirstLogin>
<PwdNeverExpire>1</PwdNeverExpire>
<PwdLastModifyTime>2013-07-10T13:40:48.470</PwdLastModifyTime>
</UserTable>
<UserTable>
<UserDBID>2</UserDBID>
<UserID>cshsvc </UserID>
<UserName>cshsvc</UserName>
<Password>54C0FCD2B54CCE8BE6AD</Password>
<FullName />
<LocalFullName />
<Title />
<Department />
<UserRole>Administrator</UserRole>
<IsFirstLogin>0</IsFirstLogin>
<PwdNeverExpire>1</PwdNeverExpire>
<PwdLastModifyTime>2013-07-10T13:38:25.070</PwdLastModifyTime>
</UserTable>
</Root>
SelectEmp_Id,Emp_Namefrom tblEmployee WhereEmp_Id<3For XML AUTO,ELEMENTS
--For XML [MODE],ELEMENTS
MODE
AUTO, RAW, EXPLICIT
Result [AUTO]
<tblEmployee><Emp_Id>1</Emp_Id><Emp_Name>AAA</Emp_Name></tblEmployee><tblEmployee><Emp_Id>2</Emp_Id><Emp_Name>BBB</Emp_Name></tblEmployee>
Result [RAW]
Above Xml with Instead of <tblEmployee>
here..... <ROW>
and </tblEmployee>
is </ROW>
SELECT ( SELECT 'White' AS Color1,
'Blue' AS Color2,
'Black' AS Color3,
'Light' AS 'Color4/@Special',
'Green' AS Color4,
'Red' AS Color5
FOR
XML PATH('Colors'),
TYPE
),
( SELECT 'Apple' AS Fruits1,
'Pineapple' AS Fruits2,
'Grapes' AS Fruits3,
'Melon' AS Fruits4
FOR
XML PATH('Fruits'),
TYPE
)
FOR XML PATH(''),
ROOT('SampleXML')
GO