2013-05-04 172 views
0

我的items变量是count:0我真的不明白为什么,xdoc包含xml文件 任何人有任何想法,为什么我不能填写items?我在这里做错了吗?为什么不能用xml值填充我的列表变量?

   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文件看起来像我只花了SNIPPIT从中疗法是多个项目:

<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网址是问题的评论,如果你想测试一下

+0

是给它太多的根元素的异常? – 2013-05-04 01:06:38

+0

没有例外的“items”变量计数是0 – Obsivus 2013-05-04 01:09:38

+0

把它放在try catch中,我认为它在第1行给出错误,表示rdf是一个未声明的前缀。第1行,第7位。' – 2013-05-04 01:14:23

回答

0

你要做这样

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

谢谢你,这是从我的代码有很大的不同,顺便说一句,如果你删除DC:和内容:其他2行将工作:) – Obsivus 2013-05-04 02:02:04