2013-05-04 171 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