One of the new features in Microsoft Visual Studio 2008 is Language Integrated Query, or LINQ for short. LINQ is a set of extensions to both the Visual Basic and C# programming languages that makes it easier for you to query any type of data, whether its in a database, an XML document, or an in-memory collection of objects.
This week, I've been preparing a sample project for a presentation I'm doing at my local .Net user group. I needed to parse an XML document and create some objects based on the elements within the document, so I decided to see if all the hype around LINQ was really true.
I have to say it was even easier than I anticipated. As an example, given the following XML document:
<?xml version="1.0" encoding="utf-8" ?>
<Products>
<Product ID="17" Name="Alice Mutton" />
<Product ID="3" Name="Aniseed Syrup" />
<Product ID="40" Name="Boston Crab Meat" />
</Products>
Let's say you want to convert this into a collection of Product objects, where a Product is defined as:
Public Class Product
Private _id As Integer
Private _name As String
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
All you have to do to convert from one to the other is use the following code snippet (add the System.Xml and System.Xml.Linq namespaces to the project first):
Module Module1
Sub Main()
Dim productXml As XDocument = XDocument.Load("Products.xml")
Dim products = From product In productXml.<Products>.<Product> _
Select New Product _
With {.ID = product.@ID, .Name = product.@Name}
For Each p As Product In products
Console.WriteLine(String.Format("Product ID: {0}, Name: {1}", p.ID.ToString, p.Name))
Next
Console.ReadLine()
End Sub
End Module
In fact, the code can be simplified even further, because you don't even need the Product class at all:
Module Module1
Sub Main()
Dim productXml As XDocument = XDocument.Load("Products.xml")
Dim products = From product In productXml.<Products>.<Product> _
Select product
For Each p In products
Console.WriteLine(String.Format("Product ID: {0}, Name: {1}", p.@ID, p.@Name))
Next
Console.ReadLine()
End Sub
End Module
If you are currently doing a lot of work with XML and are still using Visual Studio 2005, I would advise you to upgrade straight away, because the productivity gains you'll realise will be more than worth the price.