dimanche 18 mai 2014

SQL server 2008 - fragments de XML de consommer avec Linq2Sql - Stack Overflow


I have a bunch of stored procedures on SQL Server, each with an output parameter @res_xml xml output. Each of these would return an XML fragment in @res_xml.


By "fragment" I mean the piece of XML is not finalized and may easily contain several root nodes (ConformanceLevel.Fragment).


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.


However, Linq2Sql doesn't support that. It would map an XML parameter to a single XElement, and that would throw on an XML fragment.


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.


It is advised that XML parameters that may contain fragments are mapped to String instead. I'm OK with that, but is there a way to instruct linq2sql map xml to string and not to XElement?




What I do currently is:



  • Drag the procedures from the server explorer into the linq2sql designer. The code is generated.

  • Go into DataClasses.designer and copy the generated code for calling the procedures.

  • Paste this code into the partial class DataClassesX.

  • Manually replace all System.Xml.Linq.XElement with System.String.

  • Go back to the designer and delete the dragged-in procedures.


This works, but is annoying.
I can't just replace in-place, as that would get overwritten by the designer.



I have a bunch of stored procedures on SQL Server, each with an output parameter @res_xml xml output. Each of these would return an XML fragment in @res_xml.


By "fragment" I mean the piece of XML is not finalized and may easily contain several root nodes (ConformanceLevel.Fragment).


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.


However, Linq2Sql doesn't support that. It would map an XML parameter to a single XElement, and that would throw on an XML fragment.


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.


It is advised that XML parameters that may contain fragments are mapped to String instead. I'm OK with that, but is there a way to instruct linq2sql map xml to string and not to XElement?




What I do currently is:



  • Drag the procedures from the server explorer into the linq2sql designer. The code is generated.

  • Go into DataClasses.designer and copy the generated code for calling the procedures.

  • Paste this code into the partial class DataClassesX.

  • Manually replace all System.Xml.Linq.XElement with System.String.

  • Go back to the designer and delete the dragged-in procedures.


This works, but is annoying.
I can't just replace in-place, as that would get overwritten by the designer.


0 commentaires:

Enregistrer un commentaire