Using Literal XML with Embedded Expressions in VB9
Can you spot the technology usage? The Shipper class is an object relational map to the Northwind Shippers table. The Table Attribute identifies the underlying table and the ColumnAttribute identifiers the related column. The Northwind class inherits from DataContext and all you need is the connection string and LINQ to SQL is able to read and write to the database with very little code.
ConvertToXML is a generic delegate that accepts a generic List of Shipper objects and returns a (XML) string. The keyword Function(shippers) signals that you are using a Lambda expression, and <Shippers> is the opening tag of your literal XML. Yep, XML is a valid data type in VB9. The data type of the literal XML is XElement (or XAttribute) from System.Xml.Linq depending on whether the XML is an element or an attribute. The literal values are input arguments to the constructors for these types. When the compiler sees the embedded expression <%= %>, it executes that bit of code and that also is an argument to the appropriate element types constructor.
The generic delegate, Lambda expression, literal XML, and nested LINQ query statement are doing a lot. The From clause defines a range variable shipper. The range value shipper is used in the embedded expressions to obtain the element and attribute values for the XML. The Select statement of the query is actually projecting a new type, literal XML. Finally, the whole XML/LINQ expression is wrapped in parentheses and ToString returns the XML result. Whew! The output from the sample is shown in Listing 2.
Listing 2: The output from Listing 1 is XML.
<Shippers> <Shipper ShipperID="1">Speedy Distressed <Phone>(503) 555-9831</Phone> </Shipper> <Shipper ShipperID="2">United Package <Phone>(503) 555-3199</Phone> </Shipper> <Shipper ShipperID="3">Federal Shipping <Phone>(503) 555-9931</Phone> </Shipper> </Shippers>
Last night I heard a Sea World commercial. The tag line was "I believe that fun is a renewable resource". That tagline made me want to go back to Sea World, but it also resonated with me because I believe it, too. Software is supposed to be fun. And these new features are a lot of fun.
VB6 was great in its day, but there is a whole lot more fun out there just waiting to be explored and used. With Lambda expressions, LINQ to SQL, literal XML and embedded expressions, and generic delegates VB9 is not only not your father's Oldsmobile, it may very well be your son's Tesla Roadster—check out http://www.teslamotors.com/. (Sorry; I couldn't resist.)
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C# due in July 2008. Paul Kimmel is an Application Architect for EDS. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Glugnet opened a users group branch in Flint, Michigan in August 2007. If you are interested in attending, check out the www.glugnet.org web site for updates.
Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.
Page 2 of 2