简体   繁体   English

MVC应用程序中的Google Alerts RSS Feed

[英]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. 全面披露:我是MVC应用程序的新手,因此在这里我可能做不到。 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. 我的任务是开发一个MVC应用程序,该应用程序可以通过Google警报显示RSS Feed设置。 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 http://www.google.com/alerts/feeds/11811034629636691591510/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. 尝试使用Create()函数将URL加载到XmlReader以及使用Load()函数将XDocument加载到XDocument时遇到了问题。 I keep receiving an XmlException for the Uri. 我一直收到Uri的XmlException。

I am using the following as my feed data structure: 我将以下内容用作我的Feed数据结构:

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. 是的,即使我不确定原因,在尝试读取XmlReader也可能会遇到XML异常。 What you can do is get the HttpResponse . 您可以做的就是获取HttpResponse http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/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 . 现在,如果标题是动态的,则具有Xml格式的字符串。 You can parse the xml and create headers based on Xml Elements. 您可以解析xml并基于Xml Elements创建标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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