XML Output - Fields in reverse order?

  • Guys,

    For some reason the XML output given by SSRS 2008 seems to put the fields in reverse order, has anyone come across this, it seems very wierd. For example, take this simple code:

    SELECT

    1 as ID,

    'ABC Capital' as Firm_Name,

    '123 The Lane' as Firm_Address_1,

    'London' as Firm_City,

    100000 as AUM_USD,

    'Retail, Consumer Goods' as Industry

    UNION

    SELECT

    2 as ID,

    'AXyZ Capital' as Firm_Name,

    '333 The Lane' as Firm_Address_1,

    'New York' as Firm_City,

    9999 as AUM_USD,

    'Food' as Industry

    UNION

    SELECT

    3 as ID,

    'ABC Capital' as Firm_Name,

    '1 The Lane' as Firm_Address_1,

    'Milan' as Firm_City,

    555 as AUM_USD,

    'Agriculture' as Industry

    The industry field comes first and the ID field last.

    I suppose this doesn't matter but conceptually it just looks odd.

    The output given by this:

    SELECT * FROM

    (

    SELECT

    1 as ID,

    'ABC Capital' as Firm_Name,

    '123 The Lane' as Firm_Address_1,

    'London' as Firm_City,

    100000 as AUM_USD,

    'Retail, Consumer Goods' as Industry

    UNION

    SELECT

    2 as ID,

    'AXyZ Capital' as Firm_Name,

    '333 The Lane' as Firm_Address_1,

    'New York' as Firm_City,

    9999 as AUM_USD,

    'Food' as Industry

    UNION

    SELECT

    3 as ID,

    'ABC Capital' as Firm_Name,

    '1 The Lane' as Firm_Address_1,

    'Milan' as Firm_City,

    555 as AUM_USD,

    'Agriculture' as Industry

    )x

    FOR XML PATH

    In Management Studio, seems to make much more sense- it's in the same order as the select (not to mention the formatting).

    Has anyone else come across this, if so did they just put up with it, not use XML output from SSRS or do something different?

    Thanks in advance!

  • This is what I got when I used your code in SSMS as is. It looks correct for me. So perhaps something specific to your database settings (like collation) is making it sort differently?

    <row>

    <ID>1</ID>

    <Firm_Name>ABC Capital</Firm_Name>

    <Firm_Address_1>123 The Lane</Firm_Address_1>

    <Firm_City>London</Firm_City>

    <AUM_USD>100000</AUM_USD>

    <Industry>Retail, Consumer Goods</Industry>

    </row>

    <row>

    <ID>2</ID>

    <Firm_Name>AXyZ Capital</Firm_Name>

    <Firm_Address_1>333 The Lane</Firm_Address_1>

    <Firm_City>New York</Firm_City>

    <AUM_USD>9999</AUM_USD>

    <Industry>Food</Industry>

    </row>

    <row>

    <ID>3</ID>

    <Firm_Name>ABC Capital</Firm_Name>

    <Firm_Address_1>1 The Lane</Firm_Address_1>

    <Firm_City>Milan</Firm_City>

    <AUM_USD>555</AUM_USD>

    <Industry>Agriculture</Industry>

    </row>

  • You can check the collation on your db with:

    (code from Pinal Dave)

    SELECT

    c.name

    ,c.collation_name

    FROM

    sys.columns c

    WHERE

    OBJECT_ID IN

    (

    SELECT

    OBJECT_ID

    FROM

    sys.objects o

    WHERE

    type = 'U'

    AND o.name = 'YourTableName'

    )

    Just for reference, on my machine the collation is 'SQL_Latin1_General_CP1_CI_AS'.

     

  • I meant to reply yesterday afternoon, I stumbled on the solution, sort of by accident!

    On the table in question 'DataElemetStyle' was just left at the default of auto, changing this to Element had two impacts:

    1. The XML was far nicer to read - not just one massive block for each row

    2. The fields were in the correct order

    It does seem odd that in the default setting it looks hideous and the fields are inverted in their order, however, an element structure seems to cure this 🙂

    Odd but there you go, I wonder if anyone else can replicate this or if it's just me!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply