Drydo's Blog

Teenager of the Internet

This blog hosted by:
http://blogs.vbcity.com
  Home :: Syndication  :: Login   Community Forums   :: vbCity.com   :: DevCity.NET  

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

posted on Thursday, May 03, 2007 10:35 AM

Feedback

# re: RSS Feed to Dataset (Youtube Sample) 12/19/2007 3:54 AM Noam
Hi Drydo, I have been looking everywhere but couldn't find an answer to the following problem: I am trying to read a feed that has a media:thumbnail element (child of item) and i tried to following in my xsl:
<img src="{media:thumbnail/@url}" /> and it didn't work.

I use XslCompiledTransform to transform the xml document into html and I'm using the xmlns:media attribute but it looks like .Net is completely ignoring the media stuff... do you have any idea how I could get around this?

thanks,
Noam

# re: RSS Feed to Dataset (Youtube Sample) 2/15/2008 9:53 AM AlienationZombie
Great example!
*Hurries off to try it*

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: