无法使用 asp.net webform 读取 rss feed 中的所有嵌套值

问题描述 投票:0回答:1

我正在尝试使用 asp.net webform 读取 rss feed,我想定位 rss feed 中的嵌套元素。

我使用以下代码将值读取到网格中,但它针对所有标题标签并且无法读取链接值。

private void PopulateRssFeed()
        {
            string RssFeedUrl = "https://feeds.buzzsprout.com/2162687.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;
            }
        }

以下是RSS文件的完整结构

<?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>

我只想读取

items
下的值,例如
<title>Episode 1 TITLE</title>
<description></description>
<enclosure url="https://www.buzzsprout.com/2162687/827-.mp3" length="7159177" type="audio/mpeg" />
<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>

我死记硬背的代码读取主

<item>
之外的所有标题标签,我如何才能仅定位
<item>
内部的项目以及如何读取文件的URL

实时RSS提要链接https://feeds.buzzsprout.com/2162688.rss

c# xml webforms rss
1个回答
0
投票

我解决了这个问题,因为我发现很难读取 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

我更改了以下代码

link = x.Element("enclosure").Value,
link =  x.Element("enclosure").Attribute("url").Value,

© www.soinside.com 2019 - 2024. All rights reserved.