isual Basic 9 has a new set of language features that allows developers to work with XML in a much more productive way using a new API called "LINQ to XML."
LINQ stands for "Language Integrated Query," and it lets you write queries for objects, databases, and XML in a standard way. Visual Basic provides deep support for LINQ to XML through XML literals
and XML axis properties
. These features let you use a familiar, convenient syntax for working with XML in your Visual Basic code. LINQ to XML is a new, in-memory XML programming API specifically designed to leverage the LINQ framework. Even though you can call the LINQ APIs directly, only Visual Basic allows you to declare XML literals and directly access XML axis properties. This article will help you master these new Visual Basic XML features.
XML Literals and Embedded Expressions
Traditionally, when working with XML in code you would use the XML Document Object Model (XML DOM), and call its API to manipulate objects representing the structure of your XML. This API is document-centric, which does not lend itself well to creating XML with LINQ. In addition it uses the XPath language, which is unnecessary when using LINQ—as you will soon see. Another way to work with XML would be to drop out of Visual Basic code altogether and manipulate XML using XSLT, XQuery, or XPath directly.
With the release of the .NET Framework 3.5, the LINQ to XML API allows developers to work with XML in a much more intuitive way, taking a top-down, element-centric approach. Additionally, in Visual Basic 9 (VB9) you can use XML literals to embed XML directly into your code and bring an even more WYSIWYG approach to XML programming. For example, the following code is perfectly legal in VB9:
Dim myXml = <?xml version="1.0"?>
<node>This is XML</node>
The runtime infers that the myXml
variable is an XDocument
object in this case (XDocument is a class in the LINQ to XML API that, in many cases, performs much better than the XML DOM). Type inference is another new feature of VB9 that supports LINQ. The compiler can infer the types of local variables by evaluating the right-hand side of the assignment. In previous versions of Visual Basic, the preceding statement would produce a type of Object; but with the new Option Infer
flag set to ON
in VB9, the statement above is now the same as declaring the XDocument type explicitly, so your code still benefits from compile-time checks. As you can see there is no conceptual barrier between the code you write and the XML you're trying to express when using XML literals in VB9.
In the editor, you can see XML syntax coloring, which indicates that Visual Basic recognizes this XML literal as a supported data type. As you type the beginning tag of the element, Visual Basic will automatically insert the end tag and format the XML. If you change the name of the element's beginning tag, Visual Basic updates the end tag to match. Also note that underscores are not necessary in multiline XML literals.
XML literals get much more interesting when you use them with embedded expressions
. Embedded expressions let you write any Visual Basic expression and have it evaluated directly in the literal. These expressions are compiled code, not script, so you benefit from the same compile-time syntax checks and editor experience you're accustomed to when writing programs in Visual Basic. The syntax you use is <%= expression %>
. For example, using the simple literal above you could embed an expression in the <node>
element to write out the current date and time inside the element instead:
Dim myXml = <root>
<node><%= Now() %></node>
Notice that this example doesn't start with the XML declaration; instead VB9 infers this to be an XElement
object, the fundamental class in the LINQ to XML API. XML trees are made up of these XElement objects. This is just a simple example; there's no practical limit to the number of embedded expressions you can include in XML literals. You can also nest embedded expressions any number of levels deep.
Here's an example of a LINQ query that queries the files in the current directory and creates an XML output document using XML literals and embedded expressions.
Dim myXml = _
<%= From FileName In _
Let File = _
<%= File.IsReadOnly %>>
<%= File.Name %>
<%= File.CreationTime %>
<%= File.LastWriteTime %>
<%= File.DirectoryName %>
The preceding code produces XML something like this:
Notice in the example that you first start writing an XML literal and then embed a LINQ query that selects a collection of XElements <file>
to create the document. You can nest embedded expressions this way to create very complex documents in a much more intuitive way. This example creates the XML from the top down—just as you would read it. This feature lets you be much more productive using the LINQ to XML API than its predecessor, the XML DOM, which forced you to think in terms of its API instead of the actual XML you're trying to express.
|Editor's Note: This article was first published in the July/August 2008 issue of CoDe Magazine, and is reprinted here by permission.|