LinqtoXML解析RSS内容:编码

时间:2009-05-12 14:38:26

标签: asp.net-mvc linq-to-xml

我正在尝试使用Linq解析RSS提要,如下所示:

        XNamespace slashNamespace = "http://purl.org/rss/1.0/modules/slash/"; 
        XDocument rssFeed = XDocument.Load(@"http://blog.daimokuchart.com/index.php/feed/");

        var posts = from item in rssFeed.Descendants("item")
                    select new RSSData {
                        Title = item.Element("title").Value,
                        Published = DateTime.Parse(item.Element("pubDate").Value),
                        Url = item.Element("link").Value,
                        Content = item.Element("content:encoded").Value
                    };

然而;它与内容有问题:编码项我收到此错误“''''字符,十六进制值0x3A,不能包含在名称中。”

我如何解析这个item元素?

3 个答案:

答案 0 :(得分:8)

XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";

// ...

Content = item.Element(nsContent + "encoded").Value

// ...

答案 1 :(得分:1)

解析RSS提要有一种更简单的方法:SyndicationFeed

更多详情here

答案 2 :(得分:0)

您好我使用了Linqtoxml并成功解析了Rss feed尝试下面的代码

public apheadlines()
        {
            InitializeComponent();
            InitializeComponent();
            WebClient downloader = new WebClient();
            Uri rssurl = new Uri("http://ibnlive.in.com/ibnrss/rss/southcinema/telugunews.xml", UriKind.Absolute);
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloads);
            downloader.DownloadStringAsync(rssurl);
        }


 private void downloads(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Result == null)
            {
                MessageBox.Show("Error in download");
            }
            var Rss = from rss in XElement.Parse(e.Result).Descendants("item")
                      select new Data
                      {
                          Titles = rss.Element("title").Value.ToUpper(),
                          pubDate = rss.Element("pubDate").Value.Substring(0, 17)

                      };

            listBox1.ItemsSource = Rss;

        }