首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能用XML值填充我的列表变量?

为什么我不能用XML值填充我的列表变量?
EN

Stack Overflow用户
提问于 2013-05-04 08:40:32
回答 1查看 154关注 0票数 0

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

代码语言:javascript
复制
                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文件的样子,我只是从其中摘录了一小段,还有更多的项目:

代码语言:javascript
复制
<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文件的顶部:

代码语言:javascript
复制
<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在问题的注释中,如果您想测试它的话。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-04 09:56:35

你必须这样做

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16369455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档