Sharpening Your Axis with Visual Basic 9 Visual Basic 9 in Visual Studio 2008 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 allows you to write queries for things like objects, databases, and XML in a standard way. Visual Basic provides deep support for LINQ to XML through what’s called XML literals and XML axis properties. These features allow you to 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 features for working with XML in Visual Basic. XML Literals and Embedded Expressions Traditionally to work with XML in code you would use the XML Document Object Model (XML DOM) and use 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 to working with XML. Additionally, in Visual Basic 9 you can use XML literals in order to embed XML directly into your code and bring an even more WYSIWYG approach to XML programming. For instance, this is perfectly legal in Visual Basic 9: Dim myXml = <?xml version="1.0"?> <root> <node>This is XML</node> </root>
The variable is inferred to be an XDocument object in this case. Type inference is another new feature of Visual Basic 9 in order to support LINQ. Types of local variables can be inferred by the compiler by evaluating the right-hand side of the assignment. The above statement in previous versions of Visual Basic would produce a type of Object. With the new Option Infer ON in Visual Basic 9, the statement above is now the same as declaring the XDocument type explicitly so your code still benefits from compile-time checks. XDocument is one of the classes in the new LINQ to XML API and in many cases performs much better than the XML DOM. 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 Visual Basic 9. Notice the syntax coloring of the XML in the editor indicating that Visual Basic recognizes this XML literal as a supported data type. As you type the begin tag of the element, Visual Basic will automatically insert the end tag and format the XML. If you change the name of the element begin tag, Visual Basic will update 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 allow you to write any Visual Basic code and have it evaluated directly in the literal, and you can use any Visual Basic expression. These expressions are compiled code, not script, so you can benefit from the compile-time syntax checking and editor experience just like you’re accustomed to when writing programs in Visual Basic. The syntax you use is <%= expression %>. For instance, taking the simple literal above you could embed an expression in the <node> element in order to write out the current date and time inside the element instead: Dim myXml = <root> <node><%= Now() %></node> </root>
Notice this time you did not start with the XML declaration, so instead this is inferred 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; you are not limited to the number of embedded expressions in your XML literals. You can also nest embedded expressions any number of levels deep. Let’s take an example using a LINQ query that queries the files in the current directory and creates XML 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")
This will produce 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 which selects a collection of XElements <file> in order 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 enables you to 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. | & | | 
By: Beth Massi Beth Massi is a Senior Program Manager on the Microsoft Visual Studio BizApps team who build the Visual Studio tools for Azure, Office, SharePoint as well as Visual Studio LightSwitch. Beth is a community champion for business application developers and is responsible for producing and managing content and community interaction with the BizApps team. She has over 15 years of industry experience building business applications and is a frequent speaker at various software development events. You can find her on a variety of developer sites including MSDN Developer Centers, Channel 9, and her blog www.BethMassi.com. Follow her on twitter @BethMassi

By: Avner Aharoni
Avner Aharoni is a Senior Program Manager at Microsoft and was the program manager responsible for the XML language integration in Visual Basic 9.0. Prior to that, he worked on XML and object to relational mapping and other XML features in SQL Server. Currently Avner is working on the next version of the Visual Basic language.
avnera@microsoft.com | Fast Facts | | 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. | |
|