Easy SQL to XML with LINQ and Visual Basic 2008

My latest ASP Alliance article has been published:

Easy SQL to XML with LINQ and Visual Basic 2008

In this article, I demonstrate how to create an XML file from a SQL Server 2005 database using LINQ. He provides a detailed explanation of the relevant steps with the help of source code and screenshots captured from Visual Basic 2008 Express Edition. At the end of the article, he also gives a few references where you can learn more regarding the techniques involved with LINQ.

If you’re looking for a super quick intro to using Linq to SQL and Linq to XML using Visual Basic, this might be what you’re looking for.  I’ve added links to a couple other useful references, too.

This is beautiful code

If you’ve ever listened to Hanselminutes, you’ve no doubt heard Scott
mention “code smell” or “pretty code”.  The new language features in VB9,
including LINQ and XML being a native type, make XML generation not only easy,
but beautiful.  Aside from the color scheme, look at how smooth this code
snippet is.

Dim _order As New XElement(<Order>
    <ShipperRef><%= h.UniqueRef %></ShipperRef>
    <UniqueRef><%= IIf(h.CustomerPo <> "", h.CustomerPo, h.JdeNumber) %></UniqueRef>
    <Comments><%= _orderComments %></Comments>
    <OrderType><%= h.OrderType %></OrderType>
    <Workflow><%= h.Workflow %></Workflow>
    <RORRelationship><%= h.RORRelationship %></RORRelationship>
    <Supplier><%= h.SupplierNumber.ToString %></Supplier>
    <Customer><%= h.Customer.ToString %></Customer>
    <FreightBillableParty><%= h.FreightBillableParty %></FreightBillableParty>
    <MethodOfPayment>Prepay</MethodOfPayment>
    <HAZMAT><%= h.Hazmat %></HAZMAT>
    <GroupAssignment><%= SetGroupAssignment(h.GroupAssignment) %></GroupAssignment>
    <Weight><%= _orderWeight.ToString %></Weight>
    <Volume><%= _orderCube.ToString %></Volume>
    <OrderContact><%= h.PrimaryContactAssignment %></OrderContact>
    <ShipmentContact><%= h.PrimaryContactAssignment %></ShipmentContact>
    </Order>)

My VS
2008 theme is DesertNights
, and the code was copied with CopySourceAsHtml.

Installfest Tips

Tip: Install one of the Express editions before you go.

Visual Studio 2008 Installfests are going on around the country.  We had ours in Pittsburgh today, and the place was packed.  It was fun just to tinker with a bunch of other developers around.

Most people brought laptops, which means there were varying degrees of success with the installation (usually due to incomplete removal of beta bits).  One of the longest parts of the process is the installation of the .NET 3.5 Framework.

My installation was flawless and quick.  Just after VS 2008 shipped, I installed VB.NET express, which included the lengthy framework installation.  Since I already had he framework, my installation was complete a great deal of time before anyone else’s, and I had time to bang out a quick Linq demo.

In some towns, there are little programming or example contests for swag.  The extra time could do you some good.