RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Sharpening Your Axis with Visual Basic 9 : Page 6

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.

Tips and Tricks
Now that you've mastered XML programming in Visual Basic, here are some additional uses of LINQ to XML and XML literals that you may not have thought about that will probably save you some time.

Using XML Literals Instead of Multi-line Strings
One handy use of XML literals in Visual Basic is to replace your multi-line strings with XML literals for better readability. In Visual Basic, multi-line strings quickly become cumbersome because you have to concatenate string literals with your code and use underscores for readability. Here's an example of old-style multi-line string programming:

   'Old way to work with multi-line strings
   Dim oldWay = "this is a string" & vbCrLf & _
                "with formatting" & vbCrLf & _
                "and stuff" & vbCrLf & _
                "look, underscores" & vbCrLf & _
                "         tabs too"
With VB9's built-in XML literals support, you can now easily write a bunch of text directly into the editor without having to use underscores:

   'Use XML literals to avoid underscores 
   ' and preserve formatting
   Dim newWay As String = <string>
     this is a string
     with formatting
     and stuff
     look, *no* underscores!!!
     tabs too</string>.Value
The text formatting is preserved here as well. All you have to do is get the .Value of the XElement, which is the string literal. As you can see, this is much cleaner than what you're used to in the first example. And if you still like to see your string literals in the default reddish color, you can easily change the color settings for VB XML literals by following the menu path Tools → Options $rarr; Environment $rarr; Fonts and Colors, and then selecting "VB XML Text." Set the custom color to RGB(163,21,21) to match old-style string literals. Here's another example that builds an SQL query:

   'Use it anywhere you want better readability for 
   ' multi-line string literals
   Dim query = <query>
   INNER JOIN Orders 
         ON Orders.CustomerID = Customers.CustomerID
   WHERE Customers.CustomerID = @CustomerID
   Dim cmd As New SqlClient.SqlCommand(query)
   cmd.Parameters.AddWithValue("@CustomerID", id)
Text Merging
You can use XML literals to produce a lot more than just XML. In fact, you can easily create any text-based output using XML literals and embedded expressions. Using embedded expressions lets you to perform any kind of text merging in a much cleaner manner than string concatenation in code or using String.Format, especially as the size of the text increases. Here are some simple examples:

   'Simple text-merging example using embedded 
   ' expressions
   Dim simple = <string>
      This is a simple text merge example:
      Hello, <%= Environment.UserName %></string>.Value
   Dim controls = <string>
      There are the following controls on this form:
      <%= From item In Me.Controls _
      Select item.ToString & vbCrLf %>
Listing 3 shows an example that uses a simple code generation pattern to generate a Customer.vb file that describes a Customer class by reading the schema. This is a good example of having to use the descendants axis to obtain all the elements <xs:element> no matter where they appear in the document.

Working with XML in VB9 via the LINQ to XML API using XML literals and XML axis properties completely eliminates the barriers between the code you write and the XML you're trying to express. The new LINQ to XML API provides a much more intuitive approach to querying, transforming, and creating XML, and in many cases has better performance than its predecessor, the XML DOM. Say "Goodbye" to XPath, XQuery, and XSLT, and "Hello" to Visual Basic 9. For further reading, here are some additional resources you might find useful:

Beth Massi is an online content and community program manager on the Visual Studio Community Team at Microsoft, responsible for producing content for business application developers and driving community features onto MSDN Developer Centers. She also produces content on her blog, the VB Team blog and Channel 9. As a VB community champion and a long-time member of the Microsoft community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a senior architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web, and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and modifying cars.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date