简体   繁体   中英

Google Alerts RSS Feed in MVC application

Full disclosure: I am brand new to MVC applications so I could be way off in my approach here. It has been a very steep learning curve.

I am tasked with developing an MVC application that can display an RSS feed setup through Google Alerts. The following is a link to an example alert feed I've setup for "drug busts" and that I'm trying to read:

http://www.google.com/alerts/feeds/11811034629636691510/10685173545303329123

How can I load all of the "entry" fields of the feed into a data structure that can be displayed in a view? I've run into issues trying to load the URL into an XmlReader using the Create() function as well as an XDocument using the Load() function. I keep receiving an XmlException for the Uri.

I am using the following as my feed data structure:

public class FeedViewModel
{
    public FeedItem[] FeedItems { get; set; }
}
public class FeedItem
{
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime Date { get; set; }
    public string Link { get; set; }
}

I am not worried about the actual display right now, I'm just concerned about loading the feed data into the classes. Can anyone help or point me in the right direction?

Yes , you might get XML exceptions while trying to read XmlReader even I'm not completely sure why. What you can do is get the HttpResponse . http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx

   public static string HttpGet()
            {
            WebClient client = new WebClient();

            // Add a user agent header in case the  
            // requested URI contains a query.

            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            using (Stream data = client.OpenRead("http://www.google.com/alerts/feeds/11811034629636691510/10685173545303329123"))
            {
                using (StreamReader reader = new StreamReader(data))
                {
                    string s = reader.ReadToEnd();

                }
            }
        }

Now you have the string in an Xml format , If your headers are dynamic . You can parse the xml and create headers based on Xml Elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM