我正尝试使用ASP.NET Webform从RSS提要中读取。我想在RSS提要中定位嵌套元素。
我使用下面的代码将值读入网格,但它针对所有Title标记,无法读取链接值。
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>
x
<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
2条答案
按热度按时间yrdbyhpb1#
x.Element("<tag name>").Attribute("<attribute name>").Value
是从标记中的属性中提取值的方法。此外,正如注解中提到的,您不需要循环
items
中的每个项来将每个项添加到feeds
中。您可以使用该LINQ查询将数据作为
List<Feeds>
返回并分配给feeds
。字符串
oprakyz72#
我解决了这个问题,因为我发现很难读取enclose的属性值
字符串
我在寻找任何引用后更改了以下代码。
link = x.Element("enclosure").Value,
到link = x.Element("enclosure").Attribute("url").Value,