这是我正在使用的代码:
DECLARE @returnXML XML
SELECT @ReturnXML =
(SELECT [StudentID/SSN] = ssn,
[StudentName/LastName] = lastname,
[StudentName/FirstName] = firstname,
[StudentName/MiddleInitial] = Middlename,
[PrimaryAddress/Street1] = line1,
[PrimaryAddress/Street2] = line2,
[PrimaryAddress/City] = city,
[PrimaryAddress/State] = state,
[PrimaryAddress/Country] = ISNULL(country,null),
[PrimaryAddress/Zip] = zipcode
FROM StudentTable
FOR XML Path('Student'), ROOT('Root'),TYPE
)
SELECT @ReturnXML
FOR XML PATH('');
只有国家节点才需要设置 xsi:nil="true"。谢谢