XML Literals: Simplifying Strings

Posted Fri, Jul 3 2009 10:51 by Deborah Kurata

You have probably heard that the next version of VB will no longer require line continuation characters in most situations. This is very good news for those of us that do not like typing underscore characters.

But in the mean time, what do we do if we have a long string, like a SQL string:

Dim mySelect As String = "Select CustomerId, " & _
                            "LastName, " & _
                            "FirstName, " & _
                            "EmailAddress " & _
                            "From Customer " & _
                            "Where CustomerId = @CustomerID"

And this one is only selecting three fields!

XML Literals can help:

Dim mySelect2 As XElement = <string>
                            Select CustomerId,
                            LastName,
                            FirstName,
                            EmailAddress
                            From Customer
                            Where CustomerId = @CustomerId
                            </string>

Notice in the XML literals example that there are no quotes, ampersands (&) or underscores (_). That can save a lot of typing and it is easier to read.

Since mySelect2 is an XElement, use the Value property to get the string from the element.

mySelect2.Value

Enjoy!

Filed under: , ,

Comments

# DAL: Retrieve a DataTable using a SQL Statement

Tuesday, July 07, 2009 6:04 PM by Deborah's Developer MindScape

This post provides an implementation of a method that retrieves a DataTable from a SQL Server database

# DAL: Access a DataReader using a SQL Statement

Thursday, July 09, 2009 2:03 PM by Deborah's Developer MindScape

This post provides an implementation of a method that accesses a DataReader from a SQL Server database

Leave a Comment

(required) 
(required) 
(optional)
(required)