无法从 RSS feed 读取所有嵌套值

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

我正在尝试使用 ASP.NET Webform 读取 RSS 提要。我想定位 RSS 提要中的嵌套元素。

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

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;
    }
}

以下是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# asp.net xml webforms rss
2个回答
1
投票

x.Element("<tag name>").Attribute("<attribute name>").Value
是从标签中的属性中提取值的方法。

此外,正如评论中提到的,您不需要循环

items
中的每个项目来将每个项目添加到
feeds
中。

您可以使用该 LINQ 查询将数据返回为

List<Feeds>
并分配给
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();

1
投票

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