Deseralizing RSS into a Class?

G

Guest

I am trying to build an RSS class that takes information such as this: http://www.msdn.microsoft.com/rss.xml and deseralizes it into a class. I basically want the channel information such as title, description etc, but I would like a class created to function as the interface into the data. Here is my attempt:

public class RSSFeed
{
public string title;
public string link;
public string description;
public string language;
public string copyright;
public string managingEditor;
public string webMaster;
public string ubDate;
public string lastBuildDate;
public string category;
public string generator;
public string ttl;
public string rating;

public RSSFeed()
{
}

public static RSSFeed deseralize(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

RSSFeed feed = new RSSFeed();

XmlSerializer serializer = new XmlSerializer(feed.GetType());

feed = (RSSFeed)serializer.Deserialize(responseStream);

response.Close();
responseStream.Close();

return feed;
}
}

-------------------

The problem is in the Deseralize line. The XmlSeralizer throws an exception when I attempt to do this. I'm not sure why. I have tried many different ways to feed it the data without success.

There seems to be some junk chars at the front of the stream like "o:?" I'm not sure why.

But it also fails when I try a different RSS feed from slashdot (http://slashdot.org/index.rss) without the junk characters.

(And yes I do realize that I probably need XmlElement Attributes for the variables in my class to call into "//rss/channel" but I left that out for ease of reading.)

Anyone have any suggestions, or has anyone got a class that deseralizes such as this? As a workaround i can obviously load the XML into a DOM then use XPath to pull the parts I need and assign it to the variables, but that approach does not seem as elegant.
 
K

Klaus H. Probst

Kelly,

You need a schema for this. It won't work without one. I've used this:
http://apps.gotdotnet.com/xmltools/xsdinference/overview.html to do similar
things; I'm sure you can get it to work with RSS as well.


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Kelly Elias said:
I am trying to build an RSS class that takes information such as this:
http://www.msdn.microsoft.com/rss.xml and deseralizes it into a class. I
basically want the channel information such as title, description etc, but I
would like a class created to function as the interface into the data. Here
is my attempt:
public class RSSFeed
{
public string title;
public string link;
public string description;
public string language;
public string copyright;
public string managingEditor;
public string webMaster;
public string ubDate;
public string lastBuildDate;
public string category;
public string generator;
public string ttl;
public string rating;

public RSSFeed()
{
}

public static RSSFeed deseralize(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

RSSFeed feed = new RSSFeed();

XmlSerializer serializer = new XmlSerializer(feed.GetType());

feed = (RSSFeed)serializer.Deserialize(responseStream);

response.Close();
responseStream.Close();

return feed;
}
}
exception when I attempt to do this. I'm not sure why. I have tried many
different ways to feed it the data without success.
There seems to be some junk chars at the front of the stream like "o:?" I'm not sure why.

But it also fails when I try a different RSS feed from slashdot
(http://slashdot.org/index.rss) without the junk characters.
(And yes I do realize that I probably need XmlElement Attributes for the
variables in my class to call into "//rss/channel" but I left that out for
ease of reading.)
Anyone have any suggestions, or has anyone got a class that deseralizes
such as this? As a workaround i can obviously load the XML into a DOM then
use XPath to pull the parts I need and assign it to the variables, but that
approach does not seem as elegant.
 
J

Jan Tielens

You can use also RSS.NET
http://rss-net.sourceforge.net/
RSS.NET is an open-source .NET class library for RSS feeds. It provides a
reusable object model for parsing and writing RSS feeds. It is fully
compatible with RSS versions 0.90, 0.91, 0.92, and 2.0.1, implementing all
constructs.

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top