Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Sharpening Your Axis with Visual Basic 9

Visual Basic 9 completely eliminates the barrier between the code you write and the XML you're trying to express. Creating, querying, and transforming XML is much more intuitive and productive than ever before.


advertisement
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"?> <root> <node>This is XML</node> </root>

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> </root>

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 = _ <files> <%= From FileName In _ My.Computer.FileSystem.GetFiles(CurDir()) _ Let File = _ My.Computer.FileSystem.GetFileInfo(FileName) _ Select _ <file isReadonly= <%= File.IsReadOnly %>> <name> <%= File.Name %> </name> <created> <%= File.CreationTime %> </created> <updated> <%= File.LastWriteTime %> </updated> <folder> <%= File.DirectoryName %> </folder> </file> %> </files> myXml.Save("MyFiles.xml")

The preceding code produces XML something like this:

<files> <file isReadonly="true"> <name>customers.html</name> <created> 2007-10-07T15:57:00.2072384-08:00 </created> <updated> 2007-10-22T14:46:48.6157792-07:00 </updated> <folder>C:\temp</folder> </file> <file isReadonly="false"> <name>orders.xml</name> <created> 2007-10-07T15:57:00.2773392-08:00 </created> <updated> 2007-10-27T12:01:00.3549584-07:00 </updated> <folder>C:\temp</folder> </file> <file isReadonly="false"> <name>plants.xml</name> <created> 2007-10-07T15:57:00.147152-08:00 </created> <updated> 2007-10-18T18:25:07.2607664-07:00 </updated> <folder>C:\temp</folder> </file> </files>

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap