Embed Code and Avoid Underscores in Your Multiline Strings

Published 23 October 07 12:03 PM

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"

 

  MsgBox(oldWay)

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

                </string>

 

        MsgBox(newWay.Value)

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

                    </query>

 

        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 %>

                     </string>

 

        MsgBox(simple.Value)

 

 

        Dim controls = <string>

There are the following controls on this form:

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

 

        MsgBox(controls.Value)

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) %>

            Get

                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.

Enjoy,
-B

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Josh Stodola said on October 23, 2007 4:41 PM:

I have rug burn on my chin because of this post!

I simply can NOT wait for the release, this is gonna be so awesome.

# Beth Massi said on October 23, 2007 5:39 PM:

Josh,

LOL! You can give it a try right now (Beta 2) if you have an extra machine or Virtual Machine laying around: http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx

And congrats on starting your blog! ;-)

# Josh Stodola said on October 24, 2007 9:53 AM:

I'd love to give it a try but I dont an extra machine laying around.  Ok, well I do, but it is an antique unworthy of any version of VS!!

Thanks :)

I might ask around at work and see if there is a "playground" machine I can have for a while.

# Billy Hollis said on October 24, 2007 11:50 AM:

Beth, I'm stealing, er, uh, borrowing some of these examples for my "What's new in VB9" talk at DevConnections Las Vegas. Your name will be mentioned prominently.

# Beth Massi said on October 24, 2007 12:08 PM:

Hi Billy!,

For you, anything! ;-) Ping me directly if you need any more demos, decks, etc. I'll be speaking at Silicon Valley code-camp this weekend and then QCon in SF Nov 9th so I've pulling demos together this week.

-B

# Manos Kelaiditis' Weblog said on October 25, 2007 2:47 AM:

Ένα από τα νέα χαρακτηριστικά της VB.NET είναι η υποστήριξη XML Literals. Πρόκειται για τη δυνατότητα

# Beth Massi - Sharing the goodness that is VB said on October 26, 2007 4:09 PM:

A couple quick tips here when using XML literals in your Visual Basic programs. #1: I've started using

# Sven Groot said on October 27, 2007 5:19 AM:

Hi Beth,

XML literals are one of my favourite new features in VB9, really looking forward to using this in a real product.

I'm not sure I like it for multiline strings though, because of the break in indentation. It's still a pretty cool idea though. :)

# The Visual Basic Team said on November 1, 2007 3:08 PM:

We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These

# Beth Massi - Sharing the goodness that is VB said on November 2, 2007 12:02 PM:

We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These

# Beth Massi - Sharing the goodness that is VB said on December 6, 2007 9:26 PM:

I've mentioned before that you can use XML literals in VB 9 to do text merging so I figured it should

# Noticias externas said on December 6, 2007 10:01 PM:

I&#39;ve mentioned before that you can use XML literals in VB 9 to do text merging so I figured it should

# Noticias externas said on February 11, 2008 5:06 PM:

A while back I showed a very simple way you could use XML Literals to generate code. In my previous life

# Nishtha said on May 8, 2008 9:18 AM:

Hi

Can you please let us know if VB can read a text file of around 90 MB file size and process relevant data out of it.

Thanks

Nishtha

Leave a Comment

(required) 
(optional)
(required) 

About Beth Massi

Beth Massi is a Program Manager on the VS Community Team working with the Visual Basic Team producing developer content on MSDN and her blog (http://blogs.msdn.com/bethmassi). As a VB community champion and a member of the Microsoft community she helps run a .NET user group in the San Francisco Bay Area and is a frequent speaker at various software development events. Before Microsoft she was a Senior Systems Architect at a health care software product company and was a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks using Visual Basic.NET, ASP.NET, SQL-Server, and Visual FoxPro. She has worked on various projects including developing object-oriented middle-tier frameworks, COM, .NET, Web and Windows-based applications using Microsoft development tools for a variety of businesses. She loves teaching, mountain biking, and modifying cars.

This Blog

Syndication

Page view tracker