uses the STUFF function, more concise than using CASE
SQL queries can be returned as XML - this article describes some of the formatting options. Of special interest is the use of XPath to control the structure of the XML.
Foundational information about T-SQL