Retrieve an RSS feed in Visual C# 2005 : Problem with the first line (<?xml version ...)

Hi there,

My aim is to write a widget in C# 2005 to retrieve an RSS feed.

I found the following code in internet (
but I can't manage to make it work.

The stream I get contains

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" ...

and I get an exception saying "Root element missing" in line

    while (xml.Read()

I imagine the line "<?xml ..." messes things up.

Is there a simple way to get rid off this line in the stream ?

Thanks in advance
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create("http://MyAddress");
            HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
            StreamReader rss = new StreamReader(
             webresp.GetResponseStream(), Encoding.ASCII);
            if (rss != null)
                ParseRSS(rss.BaseStream); //Close the stream from the server
        public void ParseRSS(Stream rss)
            XmlTextReader xml = null;
            xml = new XmlTextReader(rss);
            while (xml.Read())
                if (xml.Depth == 2 && xml.Name == "title" &&
                    MessageBox.Show("title : " + xml.Value.ToString());
                if (xml.Depth == 2 && xml.Name == "description" &&
                    MessageBox.Show("description : " + xml.Value.ToString());

Open in new window

Who is Participating?
CoyotesITConnect With a Mentor Commented:
Hey there.
I have played around with this before and built my own readers/creators for Rss, but I have to say that once I stumbled upon this it made life a lot easier, with much less coding to get the data I was going for.

Take a look here at this project on sourceforge and see if this can save you time and get you what you want.

You will need to download the nightly tar and build the project, then just add the RSS.NET dll to as a reference to your project.

Then use for example:

private void ParseRss()
    Rss.RssFeed feed = Rss.RssFeed.Read("");
    Rss.RssChannel channel = feed.Channels[0];
    foreach (Rss.RssItem item in channel.Items)
        Response.Write(String.Format("<a href='{0}' title='{1}'>{1}</a><br />", item.Link, item.Title));

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.