/Home /Archive /Syndicate /Blog /Support /About /Contact  
All Visual Basic Feeds in one place!





Quick background - I haven't much XML experience. Sure I know what it is, I love using it with datasets and can navigate an XML document if pushed. However, I know there's an awful that it can do that I simply haven't had time to probe.

So, a post came up in the VBCity forums the other day about taking a RSS and passing that data into a dataset. "Unlikely" I scoffed and it wasn't until the next day biking into work (some 11 miles you know) that it occured to me that why shouldn't it? OK - I'm sure this is something that LINQ could do standing on its head - but why couldn't you convert the XML data using an XML Stylesheet into the format that the Dataset.ReadXML method requires...

Anyhoo, I created a quick application with one button and one datagrid. I cobbled together the XSLT document from my scant knowledge and from a couple of tutorials and save it into the BIN directory. XSLT source...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:for-each select="rss">
<xsl:for-each select="channel">
<rssItems>
<xsl:for-each select="item">
<item>
<title>
<xsl:value-of select="title"/>
</title>
<link>
<xsl:value-of select="link"/>
</link>
<description>
<xsl:value-of select="description"/>
</description>
<guid>
<xsl:value-of select="guid"/>
</guid>
</item>
</xsl:for-each>
</rssItems>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:transform>

Using this I could hopefully transform an RSS feed, e.g. the YouTube recently added list, and load that into a dataset. So, by performing a HttpWebRequest - obtaining the xml data, passing that to an XML Document. Then loading the XSLT into an XSLCompiledTransform object (Note: XMLTransform is depreciated in .NET 2.0 - uncomment the line as appropriate) I could perform the transform outputting the results into a memorystream and then loading that into the dataset using the ReadXML method....

Code CopyHideScrollFull
' Make the request to youtube and retreive its response
Dim webReq As Net.HttpWebRequest = Net.WebRequest.Create("http://youtube.com/rss/global/recently_added.rss")
Dim
webRes As Net.HttpWebResponse
' Configure

With
webReq
.Method = "GET"
.Timeout = 10000 ' Milliseconds
End With
' Obtain the response

webRes = webReq.GetResponse()
' Read the response into a string
Dim
sReader As New IO.StreamReader(webRes.GetResponseStream)
Dim
xmlData As String = sReader.ReadToEnd
' Close the request and cleanup
sReader.Dispose()
sReader = Nothing
webRes.Close()
webRes = Nothing
webReq = Nothing
' Take the xml data and convert into an XML Doc
Dim
xmlDoc As New Xml.XmlDocument
'xmlDoc.Load(Application.StartupPath & IO.Path.DirectorySeparatorChar & "recently_added.rss")

xmlDoc.LoadXml(xmlData)
' Load the xslt document to transform the XML stream
' Note: Use 'XslTransform' in VS 2003.  However, this is depreciated in VS2005
'Dim xmlTrans As New Xml.Xsl.XslTransform

Dim
xmlTrans As New Xml.Xsl.XslCompiledTransform
xmlTrans.Load(Application.StartupPath & IO.Path.DirectorySeparatorChar & "YouTubeConvert.xslt")
' Generate output stream for the convert XML data
Dim
outputStream As New IO.MemoryStream
Dim
argsList As New Xml.Xsl.XsltArgumentList
xmlTrans.Transform(xmlDoc, argsList, outputStream)
' Reset the position of the stream and add to a dataset
outputStream.Position = 0
Dim
ds As New DataSet
ds.ReadXml(outputStream)
' Simple display code using a DataGridView
Me
.DataGridView1.DataSource = ds
Me
.DataGridView1.DataMember = ds.Tables(0).TableName
. . .

...And there you go. Obivously, the XSLT doesn't identify every element and I guess when I've got time I'll revisit the XSLT to include all appropriate RSS 2.0 spec'ed objects. And ideally the webrequest should be made asynchronously but duty beckons...

M

© 2005 Serge Baranovsky. All rights reserved.
All feed content is property of original publisher. Designated trademarks and brands are the property of their respective owners.

This site is maintained by SubMain(), a division of vbCity.com, LLC