简体   繁体   English

XDocument 使用命名空间(RSS 提要)获取元素值

[英]XDocument get Element value with Namespace (RSS Feed)

Im working on getting some values from an RSS feed but i am having difficulties getting a value which has the namespace in the element tag.我正在努力从 RSS 提要中获取一些值,但我在获取元素标签中具有命名空间的值时遇到了困难。 I've tried adding the namespace to the lookup of the value but i always get null我尝试将命名空间添加到值的查找中,但我总是得到 null

Any idea on how this is achieved?关于如何实现的任何想法?

Feed https://wegotthiscovered.com/movies/feed/饲料https://wegotthiscovered.com/movies/feed/

Element xmlns:content="http://purl.org/rss/1.0/modules/content/"元素 xmlns:content="http://purl.org/rss/1.0/modules/content/"

Namespace content:encoded命名空间内容:编码

 public async Task<bool> GetNewsFeeds()
    {
        Database db = new Database();

        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("https://wegotthiscovered.com/movies/feed/", "Movie");
        dictionary.Add("https://wegotthiscovered.com/blu-ray/feed/", "Blu-ray");
        dictionary.Add("https://wegotthiscovered.com/reviews/feed/", "Reviews");
        dictionary.Add("https://wegotthiscovered.com/featured/feed/", "Featured");
        dictionary.Add("https://wegotthiscovered.com/galleries/feed/", "Galleries");

        db.DeletMovieNews();

        foreach (var pair in dictionary.ToList())
        {
            try
            {
                if (PhysicalDevice.HasInternetConnection())
                {
                    XDocument doc = XDocument.Load(pair.Key);
                    XNamespace nsSys = "http://purl.org/rss/1.0/modules/content/";
                    var entries = (from item in doc.Descendants("item")
                                  select new Movie_News
                                  {
                                      Content = item.Element(nsSys + "encoded").Value, // ISSUE HERE 
                                      Link = item.Element("link").Value,
                                      PublishedDate = item.Element("pubDate").Value,
                                      Title = item.Element("title").Value,
                                      Description = item.Element("description").Value,
                                      GroupName = "News",
                                      FeedName = pair.Value
                                  });

                    List<Movie_News> newsCollection = entries.ToList();
                    if (newsCollection.Count() != 0)
                    {
                        using (var rateGate = new RateGate(40, TimeSpan.FromSeconds(10)))
                        {
                            rateGate.WaitToProceed();

                            foreach (Movie_News item in newsCollection)
                            {
                                string regex = @"((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?";
                                Match match = Regex.Match(item.Description, regex);

                                if (match.Success)
                                {
                                    item.ImageUrl = match.Value;
                                    item.B64Image = await DownloadImage(item.ImageUrl);
                                }
                                
                                item.Description =  item.Description.Remove(0, item.Description.IndexOf("</div>"));
                                item.Description = item.Description.Replace("</div>","");


                                db.InsertNewsData(item);
                            }
                        }

                    }

                 
                    return true;

                }
            }
            catch(Exception ex) 
            {
                return false;
            }
        }

        return true;
    }

    
}

Typical, soon as i completed the write up, its working now典型的,一旦我完成了写作,它现在就可以工作了

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

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