RSS Feed to Dataset (Youtube Sample)
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….
sReader.Dispose()
sReader = Nothing
webRes.Close()
webRes = Nothing
webReq = Nothing
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = ds.Tables(0).TableName
outputStream.Position = 0
Dim ds As New DataSet
ds.ReadXml(outputStream)
Dim outputStream As New IO.MemoryStream
Dim argsList As New Xml.Xsl.XsltArgumentList
xmlTrans.Transform(xmlDoc, argsList, outputStream)
‘Dim xmlTrans As New Xml.Xsl.XslTransform
Dim xmlTrans As New Xml.Xsl.XslCompiledTransform
xmlTrans.Load(Application.StartupPath & IO.Path.DirectorySeparatorChar & “YouTubeConvert.xslt”)
Dim xmlDoc As New Xml.XmlDocument
‘xmlDoc.Load(Application.StartupPath & IO.Path.DirectorySeparatorChar & “recently_added.rss”)
xmlDoc.LoadXml(xmlData)
Dim sReader As New IO.StreamReader(webRes.GetResponseStream)
Dim xmlData As String = sReader.ReadToEnd webReq As Net.HttpWebRequest = Net.WebRequest.Create(“http://youtube.com/rss/global/recently_added.rss”)
Dim webRes As Net.HttpWebResponse
‘ Configure
With webReq
.Timeout = 10000 ‘ Milliseconds With
‘ Obtain the response
webRes = webReq.GetResponse()
End
…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…
from:http://blogs.vbcity.com/drydo/archive/2007/05/03/8266.aspx


