Using FOR XML with SQL2005

  • Hello All,

    I am working on a C# application that creates an xml document and then transfers it using an xsl document. Currently the raw xml document is created in the C# application, but I am able to create output direct from SQL Server (using the FOR XML AUTO, ELEMENTS) that exactly matches that of the C# Xmldocument.

    The problem is that I can't reference the column that this xml content is held in in the C# application using a variable.

    Where/how does SQL Server generate that column name 'XML_F52E2B61-18A1....' and is there any way I can set an alias for this column name?

    Alternativley, I have been trying to return this column name to my C# app on the fly using code similar to that below...

    XmlDocument resultsDoc = new XmlDocument();

    string xmlColName = dr.GetName(0);

    resultsDoc.InnerText = dr[xmlColName].ToString();

    ...and then putting it straight into the XmlDocument object, but to no avail.

    Any suggestions would be much appreciated, as i'm not too strong at either SQL or C# development.

    Cheers!!

  • You can give the query a column alias. If you don't, SQL Sever makes one up on its own, and it's not consistent.

    Ends up looking like:

    select

    (select col1, col2

    from dbo.MyTable

    where col1 = 1

    for XML) as XMLCol;

    You plug in the query you want, and the column name will end up being XMLCol (in this sample). Try that, see if it does what you want.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Yep that's sound, Thanks a lot!!

    😀

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

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