7.4. Serializing SQL Query Results into XML

SQL Server provides both a SQL-centric and an XML-centric way to generate XML from relational data. This section describes the SQL-centric approach.

Programmers that are familiar with writing SQL select queries want to easily generate XML from their query result. Unfortunately, such a serialization into XML can be done many different ways. SQL Server therefore provides different modes for the serialization with different levels of complexity and XML-authoring capability. All three modes are provided via a new select clause called FOR XML. The modes are raw, auto, nested, and explicit. The syntax of the FOR XML clause is ([ ] indicates optional, | indicates alternative):

 FOR XML (raw | (auto | nested) ...

Get XML Data Management: Native XML and XML-Enabled Database Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.