Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 3, 2009

XML Literals: Simplifying Strings

Filed under: VB.NET,XML @ 12:51 pm

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!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs