Simple clear advice in plain English

Hands on: Virtual Basic in 2008

Find out what the new year holds for Visual Basic programmers

First, start a new project in the free Visual Web Developer Express. Add a new page called blogfeed.aspx. We will be generating the content in code, so delete everything in blogfeed.aspx except the statement declaring the Page.

Now open the code-behind file and write a GenerateFeed function, returning a string. The first part of the function is the snippet of code above, creating the myXml variable, to which I added a couple of additional myitem elements. Next, declare a second XDocument variable, pasting the XML from an existing RSS feed. I used one from a WordPress blog.

I did not paste the entire feed, but stripped it down so that it only includes one item element. I also removed some non-essential elements for the sake of brevity. This is now a template for the new feed, as shown at the end of this article.

Lurking in the middle of this block of XML is some code. The first expression comes in the pubDate element:
<%= Now.ToString(“r”) %>

Using the same notation as in old-style ASP, this evaluates a VB function and inserts the result into the pubDate element.

The next block is more interesting. It uses a Linq to XML query to retrieve all the myitem elements in the first XDocument variable, called theItems.

There is a useful operator called a descendant axis property, represented by three dots (...). This returns all the elements of the given name wherever they are in the XML tree, starting from the specified element.

Further expressions retrieve the attribute values of each myitem, using the @ attribute operator, and insert them into the XML.

There are a couple of further steps before the code will run. VB will not compile the code if the namespaces are unrecognised. Fix this by adding imports statements for the namespaces, at the top of the code-behind file:
Imports modules/content/”>
Imports

Finally, add code for the Page Load event:
Response.Write(GenerateFeed)
Run the code. All being well, blogfeed.aspx now contains a feed that Internet Explorer or RSS reader recognises.

This simple example demonstrates how to manipulate XML in Visual Basic 2008 and how to transform one type of XML into another, but you could also source the RSS content from a database or elsewhere. You can also get intellisense for specific XML content, by importing or generating a schema.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

How to modify applications without the code

We find out how to work backwards, and migrate code you no longer have

Hands on: Master the Office 2007 Ribbon

How to make your macros work with Office 2007

Hands on: Visual Studio 2008

Find out what’s new in Visual Studio 2008

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Virtual drive

A set of files seen by Windows as a separate hard disk.

Great shopping deals from Computeractive