RDF to ASP.NET Control: Step 3
Create the code (goes in your aspx file codebehind) that will grab your transformed RSS stream and stream it to a DataSet, then convert to a DataView (the variables urlString would be your RDF link, such as "http://del.icio.us/rss/tag/.NET" and the stylesheet is the file name from Step 1):
public DataView RDFToDataView(string urlString, string stylesheet)
{
System.IO.Stream str = new System.IO.MemoryStream();
System.Xml.XPath.XPathDocument doc;
// Create the XslTransform.
System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
// Load the stylesheet that creates XML Output.
xslt.Load(System.Web.HttpContext.Current.Server.MapPath(stylesheet));
// Load the XML data file into an XPathDocument.
// if this is in the cache, grab it from there...
if (System.Web.HttpContext.Current.Cache["cached" + urlString] == null)
{
// No cache found.
doc = new System.Xml.XPath.XPathDocument(urlString);
// Add to cache.
System.Web.HttpContext.Current.Cache.Add("cached" + urlString, doc,
null, DateTime.Now.AddMinutes(60), TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.High, null);
}
else
{
// Cache found.
doc = (System.Xml.XPath.XPathDocument)System.Web.HttpContext.Current.Cache
["cached" + urlString];
}
// Create an XmlWriter which will output to our stream.
System.Xml.XmlWriter xw = new System.Xml.XmlTextWriter(str,
System.Text.Encoding.UTF8);
// Transform the feed.
xslt.Transform(doc, null, xw, null);
// Flush the XmlWriter and set the position of the stream to 0
xw.Flush();
str.Position = 0;
// Create a dataset to bind to the control.
DataSet ds = new DataSet();
ds.ReadXml(str);
// Close the writer and thereby free the memory stream.
xw.Close();
DataView dv = null;
try
{
dv = ds.Tables[0].DefaultView;
}
catch (Exception nothing)
{
}
return dv;
}

0 Comments:
Post a Comment
<< Home