The Gurus Guide to SQL Server Stored Procedures, XML, and HTML [Electronic resources]

Ken Henderson

نسخه متنی -صفحه : 223/ 122
نمايش فراداده

RAW Mode

RAW mode is the simplest of the three basic FOR XML modes. It performs a very basic translation of the result set into XML. Here's an example:

SELECT CustomerId, CompanyName
FROM Customers FOR XML RAW

(Results abridged)

XML_F52E2B61-18A1-11d1-B105-00805F49916B
--------------------------------------------
<row CustomerId="ALFKI" CompanyName="Alfreds
 Futterkiste"/><row
CustomerId="ANATR" CompanyName="Ana Trujillo
 Emparedados y helados"/><row
CustomerId="ANTON" CompanyName="Antonio Moreno
 Taquer?a"/><row
CustomerId="AROUT" CompanyName="Around the
 Horn"/><row CustomerId="BERGS"
CompanyName="Berglunds snabbk?p"/><row
 CustomerId="BLAUS" CompanyName="Blauer
See Delikatessen"/><row CustomerId="BLONP"
 CompanyName="Blondesddsl père et
fils"/><row CustomerId="WELLI"
 CompanyName="Wellington Importadora"/><row
CustomerId="WHITC" CompanyName=
"White Clover Markets"/><row CustomerId="WILMK"
CompanyName="Wilman Kala"/><row
 CustomerId="WOLZA"
CompanyName="Wolski Zajazd"/>

Each column becomes an attribute in the result set, and each row becomes an element with the generic name of row.

As I've mentioned before, keep in mind that the XML that's returned by FOR XML is not well formed because it lacks a root element. It's technically an XML fragment, and you must supply a root element in order for the document to be usable by an XML parser.