Sharing the goodness…
Beth Massi is a Senior Program Manager on the Visual Studio team at Microsoft and a community champion for business application developers. Learn more about Beth.
More videos »
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 %>
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
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.
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! ;-)
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.
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.
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
Ένα από τα νέα χαρακτηριστικά της VB.NET είναι η υποστήριξη XML Literals. Πρόκειται για τη δυνατότητα
A couple quick tips here when using XML literals in your Visual Basic programs. #1: I've started using
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. :)
We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These
I've mentioned before that you can use XML literals in VB 9 to do text merging so I figured it should
I've mentioned before that you can use XML literals in VB 9 to do text merging so I figured it should
A while back I showed a very simple way you could use XML Literals to generate code. In my previous life
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
So why did it take Microsoft 16 years to implement a form of multi-line strings? (Working from the original May 1991 release date for VB 1.0, Comdex/Windows World trade show in Atlanta, Georgia) The original bourne shell released with Unix Version 7 (in 1977) had here-documents.