1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - Using XML
LINQ - A Better Way to Query Data

By Dan Mabbutt, About.com

Jun 12 2008

LINQ is a Microsoft .NET way to code queries for "traversal, filter, and projection operations" against a data source. It's new in .NET Framework 3.5, but expect to see much more of it in the future! For one thing, Microsoft has made it easy for third party software vendors to extend LINQ with their own LINQ services.

You may have noticed that the code to get the values from the XML elements earlier didn't seem too efficient. In a way, it was a bit like the old sequential files. This is because the DOM does everything from the point of view of the entire document. To get the "FName" element, it was necessary to read the entire document, right down to the angle brackets, and then use a test (I used Select Case) until the right element comes up.

LINQ to XML uses a much different concept. If you want the "FName" element, it lets you get the "FName" element. So rather than ...

Dim myNode As XmlNode
Dim myXmlNodeReader As XmlNodeReader
mySigBlock.Load(SigDocument)
myNode = mySigBlock.DocumentElement.SelectSingleNode("SigBlock")
myXmlNodeReader = New XmlNodeReader(myNode)
Do While (myXmlNodeReader.Read())
   If myXmlNodeReader.NodeType = XmlNodeType.Element Then
      Select Case myXmlNodeReader.Name
         Case "FName"
... etc

... you can go right to the element ...

Dim mySigBlockXML As XElement = XElement.Load(SigDocument)
Dim myFName = mySigBlockXML...<FName>.Value

(Special note: If you're upgrading a project from an earlier version of VB to VB.NET 2008, make sure Option Infer is set On. See Option Infer - Round Three for details.)

The reason DOM coding is so much more difficult is that you have to reference the XML completely within the context of the document. LINQ lets you refer to the content that you're interested in without starting at the beginning.

The LINQ code above gives you the first (in this case, the 'only') FName element. If you want to get an IEnumerable collection of all of them, you can use this code:

Dim myFName = From FName In mySigBlockXML...<SigBlock> _
   Select FName.<FName>.Value
For Each FNameVal In myFName
   Debug.WriteLine(FNameVal)
Next

LINQ also lets you use VB code as part of an "in memory" XML document or document fragment (called an XElement in LINQ) with what are called embedded expressions. The syntax is <%= expression %>. You might recognize this as the same syntax used in ASP.NET. So, for example, if you wanted to construct a new XML document with content from the existing one, you could code ...

Dim SigSSNs = _
   <SSN>
   <%= From SSN In mySigBlockXML...<SigBlock> _
      Select SSN.<SSN> %>
   </SSN>

This creates the XElement type ...

<SSN>
   <SSN>123-45-6789</SSN>
</SSN>

Our program works much better now, but it really should do more. One thing that might be nice would be to store more than one signature block and allow the user to select the one that they want. We'll see how to use XML to do that in the next version of the program.

But before we get to that, both sequential logic and event-driven logic are used in all of the example programs we've done in this tutorial, but there's a difference that you have to be completely clear about to write your own VB Express programs. Since it's critical to understand event-driven programming, we cover that next in part 9, Programming Logic and System Architecture!

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - Using XML

©2009 About.com, a part of The New York Times Company.

All rights reserved.