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!