?
Solved

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

Posted on 2008-06-11
1
Medium Priority
?
586 Views
Last Modified: 2013-11-26
Hi there,

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

I found the following code in internet (http://www.codeproject.com/KB/macros/BlogReaderArticle.aspx)
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
                rss.Close();
            }
 
 
 
 
        public void ParseRSS(Stream rss)
        {
            XmlTextReader xml = null;
            xml = new XmlTextReader(rss);
            while (xml.Read())
            {
                if (xml.Depth == 2 && xml.Name == "title" &&
                    xml.IsStartElement("title"))
                {
                    xml.Read();
                    MessageBox.Show("title : " + xml.Value.ToString());
                    continue;
                }
 
                if (xml.Depth == 2 && xml.Name == "description" &&
                xml.IsStartElement("description"))
                {
                    xml.Read();
                    MessageBox.Show("description : " + xml.Value.ToString());
                    continue;
                }
            }
        }

Open in new window

0
Comment
Question by:B_B_Boris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
CoyotesIT earned 2000 total points
ID: 21762570
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.

http://rss-net.sourceforge.net/

You will need to download the nightly tar http://rss-net.sourceforge.net/RSS.NET.tar.gz and build the project, then just add the RSS.NET dll to as a reference to your project.

Then use for example: http://www.alexandre-gomes.com/?p=89






private void ParseRss()
{
    Rss.RssFeed feed = Rss.RssFeed.Read("http://ask.yahoo.com/index.xml");
    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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question