title: Create XML with variable element names from a data table with values and names
body: <p>I wasn't able to find a relevant post, so I decided to ask.</p>
  
  <p>I have the following table in my SQL Server database:</p>
  
  <pre><code>ID       attname    value 
  ---------------------------------
  22405543 blktradind N 
  22405543 brkref     IRVTGB2X
  22405543 buyamt     104650.2000 
  22405543 buycurref  USD 
  22405543 Buy53ref 
  22405543 Buy56ref 
  22405543 Buy57ref   IRVTBEBB
  </code></pre>
  
  <p>How can I convert this table by using FOR XML variations to a dynamic XML result based on the "attname" that each message has?</p>
  
  <p>For the excerpt above, the desired result would be:</p>
  
  <pre><code>&lt;Message id=22405543&gt;
    &lt;blktradind&gt;N&lt;/blktradind&gt;
    &lt;brkref&gt;IRVTGB2X&lt;/brkref&gt;
    &lt;buyamt&gt;104650.2000&lt;/buyamt&gt;
    &lt;buycurref&gt;USD&lt;/buycurref&gt;
    &lt;buy53ref /&gt;
    &lt;buy56ref /&gt;
    &lt;buy57ref&gt;IRVTBEBB&lt;/buy57ref&gt;
  &lt;/Message&gt;
  </code></pre>
  
  <p>Thanks</p>