skip to Main Content

I am trying to read from the RSS feed using the ASP.NET Webform. I want to target nested element in the RSS feed.

I used the following code to read values into the grid but it targets all Title tags and fails to read link value.

private void PopulateRssFeed()
{
    string RssFeedUrl = "https://feeds.buzzsprout.com/2162688.rss";
    List<Feeds> feeds = new List<Feeds>();
    try
    {
        XDocument xDoc = new XDocument();
        //XmlDocument xDoc = new XmlDocument();
        xDoc = XDocument.Load(RssFeedUrl);
        var items = (from x in xDoc.Descendants("item")
                     select new
                     {
                         title = x.Element("title").Value,
                         link = x.Element("enclosure").Value,
                         pubDate = x.Element("pubDate").Value,
                         //description = x.Element("description").Value
                     });
        if (items != null)
        {
            foreach (var i in items)
            {
                Feeds f = new Feeds
                {
                    Title = i.title,
                    Link = i.link,
                    //PublishDate = i.pubDate,
                    //Description = i.description
                };
                feeds.Add(f);
            }
        }
        gvRss.DataSource = feeds;
        gvRss.DataBind();
    }
    catch (Exception ex)
    {
        throw;
    }
}

Below is the complete structure of the RSS file:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="https://feeds.buzzsprout.com/styles.xsl" type="text/xsl"?>
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:podcast="https://podcastindex.org/namespace/1.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <atom:link href="https://feeds.buzzsprout.com/xxxx.rss" rel="self" type="application/rss+xml" />
  <atom:link href="https://pubsubhubbub.appspot.com/" rel="hub" xmlns="http://www.w3.org/2005/Atom" />
  <title>خلف</title>
  <lastBuildDate>Tue, 21 Nov 2023 10:27:48 -0500</lastBuildDate>
  <link>https://khalaf.buzzsprout.com</link>
  <language>ar</language>
  <copyright>@ Copyright</copyright>
  <podcast:locked>yes</podcast:locked>
    <podcast:guid>f33b9b75-9b19-58db-a432</podcast:guid>
    <itunes:author>Author XYZ</itunes:author>
  <itunes:type>episodic</itunes:type>
  <itunes:explicit>false</itunes:explicit>
  <description><![CDATA[<p> This is the description of podcast channel</p>]]></description>
  <itunes:keywords>keyword 1,keyword 2, keyword 3, keyword 4 </itunes:keywords>
  <itunes:owner>
    <itunes:name>Author Name XYZ</itunes:name>
  </itunes:owner>
  <image>
     <url>https://storage.buzzsprout.com/variants/2f1daa412b7c1921.jpg</url>
     <title>Author Logo</title>
     <link></link>
  </image>
  <itunes:image href="https://storage.buzzsprout.com/variants/1daa412b7c1921.jpg" />
  <itunes:category text="Business">
    <itunes:category text="Entrepreneurship" />
  </itunes:category>
  <itunes:category text="Business">
    <itunes:category text="Investing" />
  </itunes:category>
  <itunes:category text="Business">
    <itunes:category text="Management" />
  </itunes:category>
  <item>
    <itunes:title>Episode 1</itunes:title>
    <title>Episode 1 TITLE</title>
    <description><![CDATA[<p> Descrption of first episode </p>]]></description>
    <content:encoded><![CDATA[<p dir='rtl'> Something of first episode </p>]]></content:encoded>
    <itunes:author>AUthor Name</itunes:author>
    <enclosure url="https://www.buzzsprout.com/2162687/827-.mp3" length="7159177" type="audio/mpeg" />
    <guid isPermaLink="false">Buzzsprout-13106827</guid>
    <pubDate>Fri, 30 Jun 2023 10:00:00 +0400</pubDate>
    <itunes:duration>593</itunes:duration>
    <itunes:keywords></itunes:keywords>
    <itunes:season>1</itunes:season>
    <itunes:episode>8</itunes:episode>
    <itunes:episodeType>full</itunes:episodeType>
    <itunes:explicit>false</itunes:explicit>
  </item>
</channel>
</rss>

I only want to read values under items such as

<title>Episode 1 TITLE</title>
<description></description>
<enclosure url="https://www.buzzsprout.com/2162687/827-.mp3" length="7159177" type="audio/mpeg" />

and

<pubDate>Fri, 30 Jun 2023 10:00:00 +0400</pubDate>
<item>
    <itunes:title>Episode 1</itunes:title>
    <title>Episode 1 TITLE</title>
    <description><![CDATA[<p> Descrption of first episode </p>]]></description>
    <content:encoded><![CDATA[<p dir='rtl'> Something of first episode </p>]]></content:encoded>
    <itunes:author>AUthor Name</itunes:author>
    <enclosure url="https://www.buzzsprout.com/2162687/827-.mp3" length="7159177" type="audio/mpeg" />
    <guid isPermaLink="false">Buzzsprout-13106827</guid>
    <pubDate>Fri, 30 Jun 2023 10:00:00 +0400</pubDate>
    <itunes:duration>593</itunes:duration>
    <itunes:keywords></itunes:keywords>
    <itunes:season>1</itunes:season>
    <itunes:episode>8</itunes:episode>
    <itunes:episodeType>full</itunes:episodeType>
    <itunes:explicit>false</itunes:explicit>
  </item>

The Code that I wrote reads all the title tags outside of the main <item>. How can I target items inside <item> only and how can I read the URL for the file?

Live RSS feed link: https://feeds.buzzsprout.com/2162688.rss

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem as i was finding it hard to read the attribute value for enclose

    title = x.Element("title").Value,
    //link = x.Element("enclosure").Value,
    link =  x.Element("enclosure").Attribute("url").Value,
    pubDate = x.Element("pubDate").Value,
    description = x.Element("description").Value
    

    I changed following code after looking for any reference. link = x.Element("enclosure").Value, to link = x.Element("enclosure").Attribute("url").Value,


  2. x.Element("<tag name>").Attribute("<attribute name>").Value is the way to extract the value from the attribute in the tag.

    Besides, as mentioned in the comment, you don’t need to loop each item from the items to add each into the feeds.

    You can make use of that LINQ query to return the data as List<Feeds> and assign to feeds.

    feeds = (
                from x in xDoc.Descendants("item")
                select new Feeds
                {
                    Title = x.Element("title").Value,
                    Link = x.Element("enclosure").Attribute("url").Value,
                    PublishDate = x.Element("pubDate").Value,
                    //Description = x.Element("description").Value
                }
            ).ToList();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search