为什么我不能用xml值填充我的列表变量

本文关键字:填充 我的 列表 变量 xml 不能 为什么 | 更新日期: 2023-09-27 18:12:08

我的items变量是count: 0我真的不明白为什么,xdoc包含xml文件有人知道为什么我不能填满items吗?我做错什么了吗?

                var url = "http://sample.com/test.xml";
                var xdoc = XDocument.Load(url);
                var items = xdoc.Descendants("item")
                .Select(item => new
                {
                    Title = item.Element("title").Value,
                    Description = item.Element("description").Value,
                    Link = item.Element("link").Value,
                    PubDate = item.Element("dc:date").Value,
                    MyImage = item.Element("content:encoded").Value
                })
                .ToList();

这就是xml文件的样子,我只是从中截取了一个片段,还有更多的项目:

<item rdf:about="http://www.technewsworld.com/rsstory/77947.html">
    <title>iOS7 Will Be Flat by Design</title>
    <link>http://www.technewsworld.com/rsstory/77947.html</link>
    <description>Compared to recent Apple announcements, the secrecy surrounding the next release of the company's mobile operating system, iOS 7, has almost been hermetic. There have been reports of improvements in email and calendar apps, as well as a possible expansion of the operating systems' gesture library so it will match Apple's OS X products.</description>
    <dc:creator>John P. Mello Jr.</dc:creator>
    <dc:date>2013-05-03T05:00:00-07:00</dc:date>
    <dc:subject>iOS</dc:subject>
    <content:encoded>
            &lt;a href="http://www.technewsworld.com/rsstory/77947.html"&gt;&lt;img src="http://www.technewsworld.com/images/rw10542/ios" align="left" alt="" hspace="7" border="0" /&gt;&lt;/a&gt;
            Compared to recent Apple announcements, the secrecy surrounding the next release of the company's mobile operating system, iOS 7, has almost been hermetic. There have been reports of improvements in email and calendar apps, as well as a possible expansion of the operating systems' gesture library so it will match Apple's OS X products. Apple is working feverishly to get iOS 7 ready for its app makers in June, when it will host its annual Worldwide Developers Conference.

            </content:encoded>
    <dcterms:issued>2013-05-03T05:00:00-07:00</dcterms:issued>
    <dcterms:modified>2013-05-03T12:22:44-07:00</dcterms:modified>
  </item>
  <item rdf:about="http://www.technewsworld.com/rsstory/77891.html">
    <title>Adobe Photoshop Touch Is Almost Picture Perfect</title>
    <link>http://www.technewsworld.com/rsstory/77891.html</link>
    <description>With tablets possibly on track to overtake PCs within a few years, one might wonder just how that's going to happen. Can tablets really perform as well as PCs in professional environments? I for one am still unpacking my laptop when it comes to power applications like imaging. Well, Adobe claims it now delivers its core Adobe Photoshop functionality in an app for Android tablets.</description>
    <dc:creator>Patrick Nelson</dc:creator>
    <dc:date>2013-05-03T05:00:00-07:00</dc:date>
    <dc:subject>Reviews</dc:subject>
    <content:encoded><![CDATA[
            <a href="http://www.technewsworld.com/rsstory/77891.html"><img src="http://www.technewsworld.com/images/rw350660/adobe-photoshop" align="left" alt="" hspace="7" border="0" /></a>
            Can tablets really perform as well as PCs in professional environments? I for one am still unpacking my laptop when it comes to power applications like imaging. Well, Adobe claims it now delivers its core Adobe Photoshop functionality in an app for Android tablets -- and looking at the feature specs, it might be on to something. I decided to take expensive Adobe Photoshop Touch out for a hard-nosed run, and I wasn't just going to be fixing a few vacation snaps.

            ]]></content:encoded>
    <dcterms:issued>2013-05-03T05:00:00-07:00</dcterms:issued>
    <dcterms:modified>2013-05-02T21:20:32-07:00</dcterms:modified>
  </item>
  <item rdf:about="http://www.technewsworld.com/rsstory/77944.html">
    <title>ESEA Users' Systems Plundered in Bitcoin Mining Scam</title>
    <link>http://www.technewsworld.com/rsstory/77944.html</link>
    <description>The E-Sports Entertainment Association on Wednesday admitted that users' graphic cards had been hijacked to mine Bitcoin virtual currency. The mining was surreptitiously set in motion by a rogue employee without the knowledge of other ESEA staff or users of the network. ESEA is known for anti-cheat software and systems that allow players to compete in online matches.</description>
    <dc:creator>Peter Suciu</dc:creator>
    <dc:date>2013-05-02T15:22:49-07:00</dc:date>
    <dc:subject>Cybercrime</dc:subject>
    <content:encoded><![CDATA[
            <a href="http://www.technewsworld.com/rsstory/77944.html"><img src="http://www.technewsworld.com/images/rw555647/bitcoin" align="left" alt="" hspace="7" border="0" /></a>
            The E-Sports Entertainment Association on Wednesday admitted that users' graphic cards had been hijacked to mine Bitcoin virtual currency. The mining was surreptitiously set in motion by a rogue employee without the knowledge of other ESEA staff or users of the network. ESEA is known for anti-cheat software and systems that allow players to compete in online matches. Cofounder Eric 'Ipkane' Thunberg acknowledged the incident, which ironically occurred through the use of the anti-cheat software.

            ]]></content:encoded>
    <dcterms:issued>2013-05-02T15:22:49-07:00</dcterms:issued>
    <dcterms:modified>2013-05-02T15:22:34-07:00</dcterms:modified>
  </item>

在xml文件的顶部:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:admin="http://webns.net/mvcb/">

任何帮助都是非常感激的!

注意:XML url在问题的注释中,如果你想测试它

为什么我不能用xml值填充我的列表变量

你必须这样做

var url = @"http://www.technewsworld.com/perl/syndication/rssfull.pl";
var xdoc = XDocument.Load(url);
var v = xdoc.Descendants().Where(e => e.Name.LocalName == "item");
var items = v.Select(item => new
            {
                Title = item.Descendants().Where(e => e.Name.LocalName == "title").First().Value,
                Description = item.Descendants().Where(e => e.Name.LocalName == "description").First().Value,
                Link = item.Descendants().Where(e => e.Name.LocalName == "link").First().Value,
                //PubDate = item.Descendants().Where(e => e.Name.LocalName == "dc:date").First().Value,
                //MyImage = item.Descendants().Where(e => e.Name.LocalName == "content:encoded").First().Value,
            })
            .ToList();