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"
MsgBox(oldWay)
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
MsgBox(newWay)
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>
SELECT
Customers.*,
Orders.OrderDate
FROM
Customers
INNER JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
WHERE Customers.CustomerID = @CustomerID
</query>.Value
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
MsgBox(simple)
Dim controls = <string>
There are the following controls on this form:
<%= From item In Me.Controls _
Select item.ToString & vbCrLf %>
</string>.Value
MsgBox(controls)
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: