title: Consuming XML fragments with Linq2Sql
body: <p>I have a bunch of stored procedures on SQL Server, each with an output parameter <code>@res_xml xml output</code>. Each of these would return an XML fragment in <code>@res_xml</code>.</p>
  
  <p>By "fragment" I mean the piece of XML is not finalized and may easily contain several root nodes (<a href="http://msdn.microsoft.com/en-us/library/h2344bs2.aspx" rel="nofollow">ConformanceLevel.Fragment</a>).</p>
  
  <p>SQL Server has no problems with XML fragments, stores them just fine and reuses them eaisily when one stored procedure wants to include output of another stored procedure in its own output.</p>
  
  <p>However, Linq2Sql doesn't support that. It would map an XML parameter to a single <code>XElement</code>, and that would throw on an XML fragment.</p>
  
  <p>I don't want to change all the stored procedures to always wrap their output in a root node. It'd be a lot of typing, it'd require explicitly throwing this bogus root node out when reusing XMLs between stored procedures, and it would break existing, non-linq2sql clients that support fragments just fine. So it's linq that I'm going to massage.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/bb386947.aspx#TextMapping" rel="nofollow">It is advised</a> that <code>XML</code> parameters that may contain fragments are mapped to <code>String</code> instead. I'm OK with that, but <strong>is there a way to instruct linq2sql map <code>xml</code> to <code>string</code> and not to <code>XElement</code>?</strong></p>
  
  <hr>
  
  <p>What I do currently is:</p>
  
  <ul>
  <li>Drag the procedures from the server explorer into the linq2sql designer. The code is generated.</li>
  <li>Go into <code>DataClasses.designer</code> and copy the generated code for calling the procedures.</li>
  <li>Paste this code into the <code>partial class DataClassesX</code>.</li>
  <li>Manually replace all <code>System.Xml.Linq.XElement</code> with <code>System.String</code>.</li>
  <li>Go back to the designer and delete the dragged-in procedures.</li>
  </ul>
  
  <p>This works, but is annoying.<br>
  I can't just replace in-place, as that would get overwritten by the designer.</p>