{"id":4146,"date":"2020-10-14T22:14:20","date_gmt":"2020-10-14T21:14:20","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4146"},"modified":"2022-07-25T08:32:07","modified_gmt":"2022-07-25T07:32:07","slug":"c-parse-xml-rss","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-parse-xml-rss\/","title":{"rendered":"C# parse XML\/RSS"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Net.Http;\nusing System.Threading.Tasks;\nusing System.Xml.Linq;\n\nnamespace DatasourceAdapter\n{\n    public class DatasourceRepository\n    {\n        public async Task&lt;IEnumerable&lt;NewsItem>> GetRssFeed(string url)\n        {\n            var httpClient = new HttpClient();\n            var response = await httpClient.GetAsync(url);\n            using (var content = await response.Content.ReadAsStreamAsync())\n            {\n                var doc = XDocument.Load(content);\n                var items = doc.Descendants(XName.Get(\"item\"));\n                \n                return items.Select(x => {\n\n                    var title = x.Descendants().FirstOrDefault(e => e.Name == \"title\")?.Value;\n                    var description = x.Descendants().FirstOrDefault(e => e.Name == \"description\")?.Value;\n                    var link = x.Descendants().FirstOrDefault(e => e.Name == \"link\")?.Value;\n                    DateTime.TryParse( x.Descendants().FirstOrDefault(e => e.Name == \"pubdate\")?.Value, out var date);\n                    var image = x.Descendants().Where(e => e.Name == \"enclosure\")\n                            .Where(e => e.Attributes(XName.Get(\"type\")).Any(a => a.Value.IndexOf(\"image\") >= 0))\n                            .Select(e => e.Attributes(XName.Get(\"url\")).Select(a => a.Value).FirstOrDefault())\n                            .FirstOrDefault();\n\n                    var categories = x.Descendants().Where(e => e.Name == \"category\").Select(e => e.Value).ToList();\n\n                    return new NewsItem() {\n                        Title = title,\n                        Description = description,\n                        Link = link,\n                        Date = date,\n                        Image = image,\n                        Categories = categories\n                    };\n                }).ToList();\n            }\n        }\n        \n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-4146","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4146"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4146\/revisions"}],"predecessor-version":[{"id":4147,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4146\/revisions\/4147"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}