Obsivus
Obsivus

Reputation: 8359

Why cant I get the data from this xml file?

I am trying to grab a few element values from this xml file but I cant seem to fill my items..

My code looks like this:

        var url = "http://rss.yomiuri.co.jp/f/yolranking30";
        XNamespace dcM = "http://purl.org/dc/elements/1.1";
        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("date").Value
        })
        .ToList();

And this is how the xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="../../../../../css/rss/feedRss1.xsl" media="screen" type="text/xsl"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja">
  <channel rdf:about="http://app2.yomiuri.co.jp/xml/">
    <title>YOMIURI ONLINE</title>
    <link>http://www.yomiuri.co.jp/?from=ranking</link>
    <description>YOMIURI ONLINE</description>
    <items>
      <rdf:Seq>

      </rdf:Seq>
    </items>
        <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="self" href="http://rss.yomiuri.co.jp/rss/yol/ranking30" type="application/rss+xml" />
      </channel>
      <item rdf:about="http://www.yomiuri.co.jp/world/news/20130626-OYT1T01757.htm?from=ranking&amp;n=2013-06-27T01:50:16+09:00">
        <title>同性婚に不利な法律は違憲…米連邦最高裁が判決</title>
        <link>http://rss.rssad.jp/rss/artclk/rU.u5eAeiqIJ/1a997622b1ba6856fd5076908f7bd2f8?ul=glu4bJ7PJLVo4zXmLVKeaWZer255i3agVSmYkXXqyYziAceL01oXmlaDAPhmM2lVMmWxdIZHj9Rl.BCqdtrrrl8Ms_npUC8xmiv6fMwMrW1schwZC</link>
        <dc:subject>国際</dc:subject>
        <dc:date>2013-06-27T01:50:16+09:00</dc:date>
        <description><![CDATA[<p><img border="0" width="1" height="1" src="http://rss.rssad.jp/rss/artimg/rU.u5eAeiqIJ/1a997622b1ba6856fd5076908f7bd2f8"/></p>]]></description>
      </item>
      <item rdf:about="http://www.yomiuri.co.jp/world/news/20130626-OYT1T01205.htm?from=ranking&amp;n=2013-06-27T00:25:21+09:00">
        <title>新疆ウイグル自治区で警察など襲撃、27人死亡</title>
        <link>http://rss.rssad.jp/rss/artclk/rU.u5eAeiqIJ/4cc414b33f7e276d08a3b17388567cb9?ul=hGxozUCqWTHRxCERXg6mAg51c1EJeOT1QhnzUajt0vK9dXjQzMCnYuenvoFoFRHX0ORDlQ3HRycE1drU.20cRgB3s.__I7QxBWTMdbI4cpBxMQN93</link>
        <dc:subject>国際</dc:subject>
        <dc:date>2013-06-27T00:25:21+09:00</dc:date>
        <description><![CDATA[<p><img border="0" width="1" height="1" src="http://rss.rssad.jp/rss/artimg/rU.u5eAeiqIJ/4cc414b33f7e276d08a3b17388567cb9"/></p>]]></description>
      </item>
    </rdf:RDF>

Am I doing anything wrong here, perhaps it something with the rdf?

Any kind of help is appreciated!

Upvotes: 1

Views: 368

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Only <date> elements are in dc namespace. Default namespace for your document is

xmlns="http://purl.org/rss/1.0/"

Also you should use namespace when selecting nodes in form of ns + node_local_name

XNamespace ns = "http://purl.org/rss/1.0/";
XNamespace dc = "http://purl.org/dc/elements/1.1/";

var items = from item in xdoc.Descendants(ns + "item")
            select new {
                Title = (string)item.Element(ns + "title"),
                Description = (string)item.Element(ns + "description"),
                Link = (string)item.Element(ns + "link"),
                PubDate = (string)item.Element(dc + "date") // date in dc
            };

And last trick is casting element to string instead of accessing it's Value property. This allows to avoid exception if element is not found in document.

Upvotes: 2

Richard Seal
Richard Seal

Reputation: 4266

It could be a namespace issue. You are creating an XNamespace object but then not using it. Try getting the items using the namespace like this:

var items = xdoc.Descendants(dcM + "item").Select(item => new
    {
        Title = item.Element("title").Value,
        Description = item.Element("description").Value,
        Link = item.Element("link").Value,
        PubDate = item.Element("date").Value
    })
    .ToList();

Upvotes: 0

Related Questions