Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. FOR XML EXPLICIT driving me nuts!

FOR XML EXPLICIT driving me nuts!

Scheduled Pinned Locked Moved Database
databasexmlhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #1

    Ive done this so many times, I just cant seem to get my head around what's wrong! I have a table with data which looks like this Columns (in order): siteNo, cardSchemeCode, currencyCode, saleAmount, saleTotal, refundAmount, refundTotal

    0000001 DEL GBP 12 240.05 0 0.00
    0000001 JCB GBP 1 20.00 0 0.00
    CU1000000001 DEL GBP 10 166.51 0 0.00

    I am trying to get Xml that looks like:

    <Root>
    <Site siteNo="0000001">
    <Currency currencyCode="GBP">
    </Site>
    <Site siteNo="CU1000000001">
    <Currency currencyCode="GBP">
    </Site>
    </Root>

    Instead I get:

    <Root>
    <Site siteNo="0000001" />
    <Site siteNo="CU1000000001">
    <Currency currencyCode="GBP" />
    <Currency currencyCode="GBP" />
    </Site>
    </Root>

    Here is my SQL:

    SELECT 1 TAG,
    NULL Parent,
    Site.siteNo 'Site!1!siteNo',
    NULL 'Currency!2!currencyCode'
    FROM records [Site]
    GROUP BY siteNo
    UNION
    SELECT 2 TAG,
    1 Parent,
    Site.siteNo,
    Currency.currencyCode
    FROM records [Currency]
    INNER JOIN records [Site]
    ON Currency.siteNo = Site.siteNo
    GROUP BY Site.siteNo, Currency.currencyCode
    FOR XML EXPLICIT, ROOT('Root')

    Any help?

    W 1 Reply Last reply
    0
    • J J4amieC

      Ive done this so many times, I just cant seem to get my head around what's wrong! I have a table with data which looks like this Columns (in order): siteNo, cardSchemeCode, currencyCode, saleAmount, saleTotal, refundAmount, refundTotal

      0000001 DEL GBP 12 240.05 0 0.00
      0000001 JCB GBP 1 20.00 0 0.00
      CU1000000001 DEL GBP 10 166.51 0 0.00

      I am trying to get Xml that looks like:

      <Root>
      <Site siteNo="0000001">
      <Currency currencyCode="GBP">
      </Site>
      <Site siteNo="CU1000000001">
      <Currency currencyCode="GBP">
      </Site>
      </Root>

      Instead I get:

      <Root>
      <Site siteNo="0000001" />
      <Site siteNo="CU1000000001">
      <Currency currencyCode="GBP" />
      <Currency currencyCode="GBP" />
      </Site>
      </Root>

      Here is my SQL:

      SELECT 1 TAG,
      NULL Parent,
      Site.siteNo 'Site!1!siteNo',
      NULL 'Currency!2!currencyCode'
      FROM records [Site]
      GROUP BY siteNo
      UNION
      SELECT 2 TAG,
      1 Parent,
      Site.siteNo,
      Currency.currencyCode
      FROM records [Currency]
      INNER JOIN records [Site]
      ON Currency.siteNo = Site.siteNo
      GROUP BY Site.siteNo, Currency.currencyCode
      FOR XML EXPLICIT, ROOT('Root')

      Any help?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Hi, Your query is otherwise correct, but you must add the ORDER BY clause since the FOR XML processes the row (almost) without no logic in the order they are in the result set. So the query would be:

      ...
      ORDER BY 'Site!1!siteNo',
      'Currency!2!currencyCode'
      FOR XML EXPLICIT, ROOT('Root')

      Mika

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups