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
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.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
St Helena, a 'small British village' in the mid-Atlantic, is seeking support and funding for a broadband connection
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |