0

I'm looking to take the result of a SQL Server stored procedure and transform it into XML in .NET/C#

What I'm interested in is what standard libraries there are in .NET to help with the specifics of generating XML and if there any external libraries or tricks that can help automating the transformation from result set to XML.

I've looked at the XML options within SQL Server (05) in the past but they are not powerful enough for what I require.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AJM
  • 32,054
  • 48
  • 155
  • 243
  • how are the SQL Server (what version??) XML capabilities not powerful enough?? That would definitely be my first choice - do the XML processing on the server in the first place. – marc_s Oct 30 '09 at 10:56
  • Can you show us what your table looks like, and what the desired output is?? – marc_s Oct 30 '09 at 10:57
  • It was 2005, I can't remeber the specifics but for non trivial schemas I ran into problems where by I couldn't produce everything I needed. I looked into it a lot at the time – AJM Oct 30 '09 at 10:57

2 Answers2

3

There's basically two technologies out of the box in .NET that will allow you to create XML. In both cases, you won't get around writing quite a bit of code.

1) The XmlDocument approach, e.g. the XML DOM based way of doing things. You create a XmlDocument, create nodes, set attributes, create child nodes and so forth, and save all to disk in the end.

Pros: works on .NET 1.x and up, is quite widespread and well-known Cons: is a bit "clunky", keeps you whole XML structure in memory

See more info in the MSDN docs and countless articles and blog posts on the web

2) Then there's the newer Linq-to-XML approach, where you create your document using Linq statements. This is available in .NET 3.5 and up only, and some folks love it, other hate it with a lot of passion :-)

Pros: if you like LINQ, it feels quite natural and more "direct" than the XML DOM approach Cons: only on .NET 3.5 and up

See some articles and blog posts on the topic:

Certainly lots more out there - just bing or google for "linq to xml".

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Stumbled across this discussion as well on XDocument vs XmlDocument - http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – AJM Nov 02 '09 at 10:49
  • @AJM: great link, thanks - Jon Skeet's right on the point (as per usual) – marc_s Nov 02 '09 at 11:22
1

Definitelly use System.Xml.Linq (XDocument, XElement, etc) from .NET 3.5 It's superior and easier to use. No need for an external library (for the most part)

Nestor
  • 13,706
  • 11
  • 78
  • 119