Saturday, February 16, 2008

Cool VB 9 stuff

Beautiful day outside to go to the beach, but I'm recovering from the flu, so I stayed in and thought give some of the "new" VB9 (aka VB 2008) features a try. I must say I got into the .net world thru VB.Net but for one of my projects I decided to switch to C# (also to learn it) and never looked back.

But today I found some amazing new features in VB9 and I wanted to share them. Actually one of them is plain LinQ and we covered that before but I want to comment on VB9's integrated support for XML, and probably the best way to explain it is by showing an example... take a look at the following sub

Dim orders As New OrdersCollection
orders.Fill()

Dim elems = From order As Orders In orders Where order.ShipCountry = "Brazil" _
Select <order><id><%= order.OrderID %></id><customer><%= order.CustomersCompanyName %></customer></order>

Dim doc = <?xml version="1.0" encoding="UTF-8"?><orders></orders>
doc.Root.Add(elems)

Console.WriteLine(doc.ToString())

Keep in mind that OrdersCollection is the collection generated by DeKlarit's BusinessObject Addin. Do you have any idea what this writes to the console? well... take a look at it...


Console


First of all, have you ever seen xml displayed like that in the console? not me. And in case you missed it, let me go thru the code. First, I created the collection and filled it. Then I used LinQ to query the orders shipped to Brazil and selected <order> XElements with an id tag and a customer tag, both of them filled with data. Then I created an XDocument just writing the tags, and then just added my previous collection of XElements (elems) to the document and voilá!, to ToString method of the XDocument prints out what you see in the console pic. Ain't that cool or what?!


Others things you can do with your XDocument (doc)

Console.WriteLine(doc...<order>.Count())
Console.WriteLine(doc...<order>(0)...<customer>(0).Value)

 


The first line writes how many orders are in the xml document, while the second line writes the value of the (first) customer of the the first order.

1 comment:

InnerWorkings said...

Hi,

Good snapshot of some VB9 new features. You might be interested in the following free training to expand your VB9 skills:

* New Features in C# 3.0
* New Features in VB 9.0
* LINQ to SQL

You can check it out here: http://www.innerworkings.com/promotions/75d4bd51-4bf1-4d8a-8c47-73135e44837a/visual-studio-2008-promotion

Cheers,

Brian Finnerty
http://blogs.innerworkings.com/brian-finnerty