Embed Code and Avoid Underscores in Your Multiline Strings

One of the things I most dearly missed from FoxPro when I moved to VB.NET was the ability to easily dump a bunch of text (multi-line string literals) into the editor easily and embed code (text-merge). FoxPro has a keyword TEXT…ENDTEXT for this and I used to use it all the time. In VB it gets pretty darn ugly with any large amount of text because you have to concatenate string literals with your code and use underscores for readability.

  Dim oldWay = “this is a string” & vbCrLf & _

               “with formatting” & vbCrLf & _

               “and stuff” & vbCrLf & _

               “look ma, underscores” & vbCrLf & _

                        tabs too”



Not any more. With Visual Basic 9’s built in XML literals support we can now easily write a bunch of text directly into the editor:


        Dim newWay = <string>

this is a string

with formatting

and stuff

look ma, no underscores!!!

            tabs too




The text formatting is preserved 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 we’re used to. 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 in Tools –> Options –> Environment –> Fonts and Colors, then select “VB XML Text” and set the custom color to RGB(163,21,21). Here’s another example, some SQL query text (now with the reddish color):


        Dim query = <query>

SELECT Customers.*, Orders.OrderDate

FROM Customers

    INNER JOIN Orders ON Orders.CustomerID = Customers.CustomerID

WHERE Customers.CustomerID = @CustomerID



        Dim cmd As New SqlCommand(query.Value)

        cmd.Parameters.AddWithValue(“@CustomerID”, id)

Now here’s where it gets fun. You can also embed expressions into these literals with the <%= syntax. This means you can do any kind of text merging much cleaner than ugly concatenation of strings with code or use of String.Format especially as the size of the text increases. Here’s some simple examples:

        Dim simple = <string>

This is a simple text merge example:

Hello, <%= Environment.UserName %>






        Dim controls = <string>

There are the following controls on this form:

<%= From item In Me.Controls Select item.ToString & vbCrLf %></string>



Calvin has some good examples here and here on how to generate scripts dynamically, but here’s one that uses a simple code generation pattern. 

Private Sub CreateClass()

        Dim CustomerSchema As XDocument = XDocument.Load(CurDir() & “\customer.xsd”)


        Dim fields = From field In CustomerSchema…<xs:element> _

                     Where field.@type IsNot Nothing _

                     Select Name = field.@name, Type = field.@type



        Dim customer = <customer>

Public Class Customer

    <%= From field In fields Select <f>    

        Private m_<%= field.Name %> As <%= GetVBPropType(field.Type) %></f>.Value %>


        <%= From field In fields Select <p>    

        Public Property <%= field.Name %> As <%= GetVBPropType(field.Type) %>


                Return m_<%= field.Name %> 

            End Get

            Set(ByVal value As <%= GetVBPropType(field.Type) %>)

                m_<%= field.Name %>  = value

            End Set

        End Property</p>.Value %>                       

End Class</customer>


        My.Computer.FileSystem.WriteAllText(“Customer.vb”, customer.Value, _

False, System.Text.Encoding.ASCII)


    End Sub


    Private Function GetVBPropType(ByVal xmlType As String) As String

        Select Case xmlType

            Case “xs:string”

                Return “String”

            Case “xs:int”

                Return “Integer”

            Case “xs:decimal”

                Return “Decimal”

            Case “xs:boolean”

                Return “Boolean”

            Case “xs:dateTime”, “xs:date”

                Return “Date”

            Case Else

                Return “‘TODO: Define Type”

        End Select

    End Function

I hope this gives you some good ideas on what you can do with XML Literals. You literally (pun intended ;-)) don’t have to use them to produce XML, you can use them to produce any text-based output.