Search code examples
c#xmlxml-parsingrssfeed

Why cant I fill my list variable with the xml values?


My items variable is count: 0 I dont really understand why, xdoc contains the xml file anyone have any idea why I cant fill items? Am I doing something wrong here.

                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();

This is how the xml file looks like I just took a snippit from it ther is more items:

<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>

This is in the top of the xml file:

<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/">

Any kind of help is appreciated alot!

Note: the XML url is in the comment of the question, if you want to test it out


Solution

  • You have to do this way

    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();