jump to navigation

XML Literals and Visual basic 9.0 November 21, 2008

Posted by fofo in VB 9.0, Visual Studio 2008, XML.
Tags: , ,
trackback

In this post I would like to demonstrate with some examples a new feature of Visual Basic 9.0 which is XML Literals.C# does not support XML Literals.

If you want to find out about the reasons that VB chose to support XML Literals and C# did not, click here

If you want to look more into this have a look here as well.

Basically if you are a C# fanatic (as many people out there), If you have a project that relies heavily on XML, you will be better off if you choose VB 9.0 instead. Or if it is a mixed project you can use VB 9.0 in parts of a project where literal XML would be useful, and C# in the parts of the project…

In the past If we wanted to have an XML document from code we had to use API’s to create elements and attach attributes…

In VB 9.0 you can create XML documents in code without using strings or APIs.

We need to start a new project in Visual Studio to examine some of these new features.

1) Launch Visual Studio 2008

2) Start a new project, choose VB as your preferred language and then choose Console Application as your template

3) Name the project “XmlLiterals” or any other name of your choice

Inside the Module1.vb ,place this code

Dim thexml = <books>
<book paperback=”yes” cdrom=”no”>23</book>
</books>Console.WriteLine(thexml)

Console.ReadLine()

If you run the application it will not break and it will print our small XML document on the screen.

We could re-write the above snippet of code differently. Comment out the lines above.

Have a look at the code below

Dim thexml = <books>
<%=<book paperback=”yes” cdrom=”no”>23</book> %>
</books>Console.WriteLine(thexml)

Console.ReadLine()

and paste it in the Module1.vb

If you run the application again, you will get the same output.

The syntax in red color is called embedded code block.

That sort of syntax allows us to replace the xml code with other values such as variables.

I would like to add a class in my module in order to demonstrate XML Literals better.

Paste the code below after the code for the module ends (End Module)

Class Book

Private _author As String

Private _pub_year As String

Private _stock As Integer

Public Property Author() As String

Get

Return _author

End Get

Set(ByVal value As String)

_author = value

End Set

End Property

Public Property Pub_Year() As String

Get

Return _pub_year

End Get

Set(ByVal value As String)

_pub_year = value

End Set

End Property

Public Property Stock() As Integer

Get

Return _stock

End Get

Set(ByVal value As Integer)

_stock = value

End Set

End Property

End Class

This class is called Book and it has 3 properties, author,pub_year,stock.

Now what I need to do is to create a collection of these objects and assign them some values.

I can do that by using a function which I call Createbooks.

Here is the code. It is pretty simple really…

Function CreateBooks() As List(Of Book)

Dim book1 As New Book() With {.Author = “Ian McEwan”, .Title = “Atonement”, .Pub_Year = 2002, .Stock = 23}

Dim book2 As New Book() With {.Author = “Sylvia Plath”, .Title = “The Colossus”, .Pub_Year = 1980, .Stock = 19}

Dim book3 As New Book() With {.Author = “Jane Austen”, .Title = “Pride and Prejudice”.Pub_Year = 1976, .Stock = 11}

Dim returnList As New List(Of Book)

returnList.Add(book1)

returnList.Add(book2)

returnList.Add(book3)

Return returnList

End Function

Place the function inside your module, just after Sub Main().

Comment out these lines

‘ Dim thexml = <books>
‘     <%= <book paperback=”yes” cdrom=”no”>23</book> %>
‘     </books>

Inside your  Sub Main() store the list of objects in a variable.

So basically type something like this

Dim mybooks = CreateBooks()

Then in order  to find all the values for each book i need to write something like this

Dim bookitems = From book In mybooks _

Select <Book Author=<%= book.Author %> Title=<%= book.Title %> Published_year=<%= book.Pub_Year %>> Stock=<%= book.Stock %></Book>

Dim theXML = <?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

<Books>

<%= bookitems %>

</Books>

Console.WriteLine(theXML)

Console.ReadLine()

After I retrieve all the elements I create a new XML document and place them in there.

Run your application. In the console window you will see the Books element and then 3 seperate book elements each with attribute and values.

In order to find out what kind of object is the “theXML” variablejust add this line of code

Console.WriteLine(TypeName(theXML))

If your run your application you will see that .Net refers to it as XDocument.


Comment out the line above.

So what if I wanted to print one item in the screen? What if I wanted to get hold of the name of the author of the first book?

This is how I navigate the XDocument object tree.

Dim myfirstbookauthor As String = theXML.<Books>.<Book>(0).@Author
Console.WriteLine(myfirstbookauthor)

Run you application and will get “Ian McEwan”.

If you wanted to loop through all the items in the object tree and get a specific attribute(like the book’s title), this is one way to do it.

For Each book In theXML.<Books>.<Book>
Console.WriteLine(book.@Title)
Next

Make sure that you comment these lines out, so you do not get confused

‘Dim myfirstbookauthor As String = theXML.<Books>.<Book>(0).@Author
‘Console.WriteLine(myfirstbookauthor)

Run you application. In the Console window you will see these values,

“Atonement”

“The Colossus”

“Pride and Prejudice”

To recap, VB 9.0 treats XML Literals like String Literals, number Literals and allows you to work directly with XML.

If you need the source code email me…

Hope it helps!!!

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Comments»

1. Create an XML file using LINQ and import XML data into an excel spreadsheet « DOT NET RULES - March 31, 2009

[…] you want to read another post of mine regarding XML Literals click here […]


Leave a comment