Monday, March 19, 2012

Generating a value list using PATH mode

I am attempting to get data using the "for xml path" and would like to list my data in child elements. I can render the data in the format that I would want but instead of creating child elements I am left with HTML like character codes around my element name (&lt and &gt).

I am following the example outlined in http://msdn2.microsoft.com/en-us/library/ms189885.aspx section C. The example unfortunately even shows the &lt and &gt symbols. I am missing something obvious here? Any help would be greatly appreciated.

Are you using a nested query? If so add the TYPE directive to the FOR XML e.g.

Code Snippet

SELECT ProductModelID as "@.ProductModelID",

Name as "@.ProductModelName",

(SELECT ProductID as "data()"

FROM Production.Product

WHERE Production.Product.ProductModelID =

Production.ProductModel.ProductModelID

FOR XML PATH ('')

) as "@.ProductIDs",

(

SELECT Name as "ProductName"

FROM Production.Product

WHERE Production.Product.ProductModelID =

Production.ProductModel.ProductModelID

FOR XML PATH (''), TYPE

) as "ProductNames"

FROM Production.ProductModel

WHERE ProductModelID= 7 or ProductModelID=9

FOR XML PATH('ProductModelData')

that way the XML is not escaped.

No comments:

Post a Comment