使用 Linq 的 RSS 提要问题

发布于 2024-08-05 02:06:57 字数 7250 浏览 2 评论 0原文

尝试使用 Linq 读取 RSS 并选择信息,但似乎无法正常工作。

这是我的代码:

XDocument feedXML = XDocument.Load(
       "http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0");

        var feeds = from feed in feedXML.Descendants("entry")
                    select new
                    {
                        Title = feed.Element("title").Value,
                        Link = feed.Element("link").Value,
                        Description = feed.Element("content").Value
                    };

这是我的 RSS 的格式:

<?xml version="1.0" encoding="UTF-8"?>    
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <id>http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0</id>
  <link href="http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0" type="application/atom+xml" rel="self" />
  <link href="http://eventful.com/performers/bliminal-/P0-001-000192452-0" type="text/html" rel="alternate" />
  <title>B-Liminal Tour Dates in 2009, B-Liminal Concert Schedule - Eventful</title>
  <subtitle>Up-to-date event feeds from eventful.com, the world's leading event website.</subtitle>
  <rights type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
      Copyright &#169; 2006
      <a href="http://eventful.com/">Eventful, Inc.</a>  All rights reserved.  Use subject to terms of use:
      <a href="http://eventful.com/terms">http://eventful.com/terms</a>
    </div>
  </rights>
  <generator version="1.0">EVDB::Atom</generator>
  <opensearch:totalResults></opensearch:totalResults>
  <opensearch:startIndex>1</opensearch:startIndex>
  <updated>2009-09-13T11:48:52+00:00</updated>
  <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
    <id>http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1</id>
    <published>2009-09-13T15:09:00+00:00</published>
    <updated>2009-09-13T15:48:12+00:00</updated>
    <link href="http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1" type="text/html" rel="alternate" />
    <gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed" />
    <gd:visibility value="http://schemas.google.com/g/2005#event.public" />
    <gd:transparency value="http://schemas.google.com/g/2005#event.transparent" />
    <title>B-Liminal FREE fan appreciation show!</title>
    <gd:when startTime="2009-09-19T02:00:00+00:00" endTime="2009-09-19T02:00:00+00:00" />
    <georss:where>
      <gml:Point>
        <gml:pos>26.9342076 -80.0922588</gml:pos>
      </gml:Point>
    </georss:where>
    <gd:where rel="http://schemas.google.com/g/2005#event" valueString="Corners near 71 East Indiantown Road , Jupiter, Florida, 33458, United States">
      <gd:entryLink>
        <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
          <id>http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9</id>
          <link href="http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9" type="text/html" rel="alternate" />
          <title>Corners</title>
          <georss:where>
            <gml:Point>
              <gml:pos>26.9342076 -80.0922588</gml:pos>
            </gml:Point>
          </georss:where>
          <gd:contactSection label="Corners">
            <gd:postalAddress>
              71 East Indiantown Road
              Jupiter, Florida  33458, United States
            </gd:postalAddress>
            <gd:geoPt lat="26.9342076" lon="-80.0922588" />
          </gd:contactSection>
          <content type="html">
            &lt;div class='vcard'&gt;
            &lt;a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'&gt;Corners&lt;/a&gt;
            &lt;div class='adr'&gt;
            &lt;span class='street-address'&gt;71 East Indiantown Road&lt;/span&gt;, &lt;span class='locality'&gt;Jupiter&lt;/span&gt;, &lt;span class='region'&gt;Florida&lt;/span&gt; &lt;span class='postal-code'&gt;33458&lt;/span&gt; &lt;span class='country-name'&gt;United States&lt;/span&gt;&lt;/div&gt;
            &lt;/div&gt;
          </content>
          <media:text type="html">
            &lt;div class='vcard'&gt;
            &lt;a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'&gt;Corners&lt;/a&gt;
            &lt;div class='adr'&gt;
            &lt;span class='street-address'&gt;71 East Indiantown Road&lt;/span&gt;, &lt;span class='locality'&gt;Jupiter&lt;/span&gt;, &lt;span class='region'&gt;Florida&lt;/span&gt; &lt;span class='postal-code'&gt;33458&lt;/span&gt; &lt;span class='country-name'&gt;United States&lt;/span&gt;&lt;/div&gt;
            &lt;/div&gt;
          </media:text>
          <category scheme="http://eventful.com/venues/tags/" term="americantowns" label="American Towns" />
          <category scheme="http://eventful.com/venues/tags/" term="americantownscom" label="americantowns.com" />
          <category scheme="http://eventful.com/venues/tags/" term="artistdatacom" label="artistdata.com" />
          <category scheme="http://eventful.com/venues/tags/" term="barnightclub" label="BarNight Club" />
          <category scheme="http://eventful.com/venues/tags/" term="community" label="community" />
          <category scheme="http://eventful.com/venues/tags/" term="local" label="local" />
          <category scheme="http://eventful.com/venues/tags/" term="townsquare" label="town square" />
          <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#contact"  />
          <author>
            <name>evdb</name>
            <uri>http://eventful.com/users/evdb</uri>
          </author>
        </entry>
      </gd:entryLink>
    </gd:where>

Trying to read an RSS and select information using Linq but can't seem to get it working.

Here is my code:

XDocument feedXML = XDocument.Load(
       "http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0");

        var feeds = from feed in feedXML.Descendants("entry")
                    select new
                    {
                        Title = feed.Element("title").Value,
                        Link = feed.Element("link").Value,
                        Description = feed.Element("content").Value
                    };

And here is the format of my RSS:

<?xml version="1.0" encoding="UTF-8"?>    
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <id>http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0</id>
  <link href="http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0" type="application/atom+xml" rel="self" />
  <link href="http://eventful.com/performers/bliminal-/P0-001-000192452-0" type="text/html" rel="alternate" />
  <title>B-Liminal Tour Dates in 2009, B-Liminal Concert Schedule - Eventful</title>
  <subtitle>Up-to-date event feeds from eventful.com, the world's leading event website.</subtitle>
  <rights type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
      Copyright © 2006
      <a href="http://eventful.com/">Eventful, Inc.</a>  All rights reserved.  Use subject to terms of use:
      <a href="http://eventful.com/terms">http://eventful.com/terms</a>
    </div>
  </rights>
  <generator version="1.0">EVDB::Atom</generator>
  <opensearch:totalResults></opensearch:totalResults>
  <opensearch:startIndex>1</opensearch:startIndex>
  <updated>2009-09-13T11:48:52+00:00</updated>
  <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
    <id>http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1</id>
    <published>2009-09-13T15:09:00+00:00</published>
    <updated>2009-09-13T15:48:12+00:00</updated>
    <link href="http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1" type="text/html" rel="alternate" />
    <gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed" />
    <gd:visibility value="http://schemas.google.com/g/2005#event.public" />
    <gd:transparency value="http://schemas.google.com/g/2005#event.transparent" />
    <title>B-Liminal FREE fan appreciation show!</title>
    <gd:when startTime="2009-09-19T02:00:00+00:00" endTime="2009-09-19T02:00:00+00:00" />
    <georss:where>
      <gml:Point>
        <gml:pos>26.9342076 -80.0922588</gml:pos>
      </gml:Point>
    </georss:where>
    <gd:where rel="http://schemas.google.com/g/2005#event" valueString="Corners near 71 East Indiantown Road , Jupiter, Florida, 33458, United States">
      <gd:entryLink>
        <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
          <id>http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9</id>
          <link href="http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9" type="text/html" rel="alternate" />
          <title>Corners</title>
          <georss:where>
            <gml:Point>
              <gml:pos>26.9342076 -80.0922588</gml:pos>
            </gml:Point>
          </georss:where>
          <gd:contactSection label="Corners">
            <gd:postalAddress>
              71 East Indiantown Road
              Jupiter, Florida  33458, United States
            </gd:postalAddress>
            <gd:geoPt lat="26.9342076" lon="-80.0922588" />
          </gd:contactSection>
          <content type="html">
            <div class='vcard'>
            <a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'>Corners</a>
            <div class='adr'>
            <span class='street-address'>71 East Indiantown Road</span>, <span class='locality'>Jupiter</span>, <span class='region'>Florida</span> <span class='postal-code'>33458</span> <span class='country-name'>United States</span></div>
            </div>
          </content>
          <media:text type="html">
            <div class='vcard'>
            <a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'>Corners</a>
            <div class='adr'>
            <span class='street-address'>71 East Indiantown Road</span>, <span class='locality'>Jupiter</span>, <span class='region'>Florida</span> <span class='postal-code'>33458</span> <span class='country-name'>United States</span></div>
            </div>
          </media:text>
          <category scheme="http://eventful.com/venues/tags/" term="americantowns" label="American Towns" />
          <category scheme="http://eventful.com/venues/tags/" term="americantownscom" label="americantowns.com" />
          <category scheme="http://eventful.com/venues/tags/" term="artistdatacom" label="artistdata.com" />
          <category scheme="http://eventful.com/venues/tags/" term="barnightclub" label="BarNight Club" />
          <category scheme="http://eventful.com/venues/tags/" term="community" label="community" />
          <category scheme="http://eventful.com/venues/tags/" term="local" label="local" />
          <category scheme="http://eventful.com/venues/tags/" term="townsquare" label="town square" />
          <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#contact"  />
          <author>
            <name>evdb</name>
            <uri>http://eventful.com/users/evdb</uri>
          </author>
        </entry>
      </gd:entryLink>
    </gd:where>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2024-08-12 02:06:57

问题在于 entry 元素位于 XML 命名空间中(每个 entry 中的 xmlns 属性)。

您需要执行此操作:

XNamespace atom = "http://www.w3.org/2005/Atom";
var feeds = from feed in feedXML.Descendants(atom + "entry")
            select new {
                Title = feed.Element(atom + "title").Value,
                Link = feed.Element(atom + "link").Value,
                Description = feed.Element(atom + "content").Value
            };

此代码使用隐式转换来创建 XNamespace 对象,然后使用 XNamespace 的重载 + 运算符创建一个 XName。有关详细信息,请参阅此处

The problem is that the entry elements are in an XML namespace (the xmlns attribute in each entry).

You need to do this:

XNamespace atom = "http://www.w3.org/2005/Atom";
var feeds = from feed in feedXML.Descendants(atom + "entry")
            select new {
                Title = feed.Element(atom + "title").Value,
                Link = feed.Element(atom + "link").Value,
                Description = feed.Element(atom + "content").Value
            };

This code uses an implicit cast to create an XNamespace object, then uses XNamespace's overloaded + operator to create an XName. For more information, see here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文