locked
Windows Store App - Parsing RSS feed RRS feed

  • Question

  • So  i am making a call to USATODAY API. It returns an RSS feed that is below.

    <rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" version="2.0">
    <channel>
    <cf:treatAs>list</cf:treatAs>
    <title>USATODAY.com Feed</title>
    <link>
    http://apidata.usatoday.com?kjnd=HlaswUT0sE1Rfogr78x20dVXBKYlYlmfFF7QydxLlhAD%2Bye509T%2F91Dc%2FsdhQh%2FP-b563b6d3-0383-4f78-bb8b-201a924382d5_3XdXDIvYR%2Fw8IJg23kUkxySkv%2FRXDMPlojf8kLTgh3KTglz2QuPTI%2FYqKlbf2umx
    </link>
    <description>
    http://apidata.usatoday.com?kjnd=td%2B37cFgQP4bSk3N%2BM5FzROVXieJ4GAMb43SMcqXTViZir40%2BITsfdfuZqlObwfC-b563b6d3-0383-4f78-bb8b-201a924382d5_TunBgAtJKf68T7a%2BE5Um3B%2FbGTNMoaA%2B4Z9iQ%2BUYiGnBM5UoNp5xg8Hk0KUqTAcZ
    </description>
    <language>en-us</language>
    <copyright>Copyright 2013, USATODAY.COM, USA TODAY</copyright>
    <lastBuildDate>Sun, 2 Jun 2013 11:31:04 GMT</lastBuildDate>
    <image>
    <title>USATODAY.com</title>
    <width>135</width>
    <height>20</height>
    <link>
    http://apidata.usatoday.com?kjnd=5nmoCsLHUdQG8JMH1Ei4B9DHhJv3zAA22Vl4daZZExoAgCENnQkLS5%2F9VA7wIcTz-b563b6d3-0383-4f78-bb8b-201a924382d5_XUO7VvPRlHPFS0rGjfkPzm9f6MwUJ0rMbqaTnR0AhZ%2B1zfBAhoF15V8O5DcFbcSy
    </link>
    <url>http://i.usatoday.net/_common/_images/usatodaycom_135x20.gif</url>
    </image>
    <cf:listinfo>
    <cf:sort element="pubDate" data-type="date" label="Date/Time Order" default="false" />
    <cf:sort element="title" data-type="text" label="Title Order" default="false" />
    </cf:listinfo>
    <item>
    <title>Anti-government protests spread across Turkey</title>
    <link>
    http://apidata.usatoday.com/story/news/world/2013/06/02/turkey-protests/2381911/?kjnd=xGgy9YG4THf1hCUgfTOMdVtx%2F6U6mcocTBzhduz1cZU5aIt8TDqz%2BoCPHdOZuaWB-b563b6d3-0383-4f78-bb8b-201a924382d5_0%2BhZXooVoTQgz56sCVNiQnLLWa9%2Fn%2BVulwrjP4Zwv3h3hSci5ECdSqGclzy61q71
    </link>
    <pubDate>Sun, 2 Jun 2013 11:25:21 GMT</pubDate>
    <description>Amnesty International said two were killed and more than 1,000 injured in clashes with police.</description>
    <guid isPermalink="true">
    http://apidata.usatoday.com/story/news/world/2013/06/02/turkey-protests/2381911/?kjnd=K96j6Bo7tSO4%2FNVBB1Ec5XB%2FkW5vzN1t40VxvNElnA%2FamzKkSyZ6DzC%2FMpiD1Ka3-b563b6d3-0383-4f78-bb8b-201a924382d5_Ktl7QwaS6acH1AXSszUzBoH9eqYJ6EVnwr3WgQ4wfqdNFsBUlSXc7pmGXf3iikhL
    </guid>
    </item>
    <item>
    <title>Wisdom teeth: Should they stay or go?</title>
    <link>
    http://apidata.usatoday.com/story/news/nation/2013/06/02/wisdom-teeth-surgery/2369593/?kjnd=efghc81Bz%2BQnlC4D2jEsmJPfakoSfyh4qZGabvzd6mC3gvFR3Njd%2Bm7aiIfPHKYB-b563b6d3-0383-4f78-bb8b-201a924382d5_11y9PzLiwu3bMAM0KC4vVfC%2BI48i3wbAJAurneGX4w%2FopRyXarM7%2FXdtAtgiyRb8
    </link>
    <pubDate>Sun, 2 Jun 2013 10:00:21 GMT</pubDate>
    <description>Some healthy wisdom teeth may be OK to keep, as long as you have regular exams and X-rays.</description>
    <guid isPermalink="true">
    http://apidata.usatoday.com/story/news/nation/2013/06/02/wisdom-teeth-surgery/2369593/?kjnd=zgVpeYtvVxehFUe77bKN6VOHpjwgjof2MMqNiOnT6l%2BC4WE4Y8PEHJt5ZNjQXo6Y-b563b6d3-0383-4f78-bb8b-201a924382d5_NgO0SytXjeslqig3zfY0mBnc4YfI91AWZ%2FLZONRsjujgR7Bj%2FpuJjuHPjgu1ZG6M
    </guid>
    </item>
    <item>
    <title>Obama aide: White House has behaved appropriately</title>
    <link>
    http://apidata.usatoday.com/story/theoval/2013/06/02/obama-valerie-jarrett-scandals-wellesely-college/2382037/?kjnd=E9qoegyiEJIWoDr%2B%2FiS2QrlUDuet7mG6Ck3gpKOaXCIuZcS4vIlf%2BlBvG91dP%2FFU-b563b6d3-0383-4f78-bb8b-201a924382d5_ipwXR2YTkhrnOoz6FOHS3NvB%2FpA1drBJLJ9ktBGAGM26cE48MAMzR4K%2F5NJA%2BgU5
    </link>
    <pubDate>Sun, 2 Jun 2013 10:55:23 GMT</pubDate>
    <description>One of President Obama's top aides defends the administration.</description>
    <guid isPermalink="true">
    http://apidata.usatoday.com/story/theoval/2013/06/02/obama-valerie-jarrett-scandals-wellesely-college/2382037/?kjnd=Dcp2MLabhzyP5Cv7zYnBzpbXDE92xuYtPOnjwySuNXuRjrfdVkoou5EBQlxtMjz8-b563b6d3-0383-4f78-bb8b-201a924382d5_KMJZWTi7Qe9VLlw6RiKtSsjEhYy9iMgnhjtWA9gymno7BJkn80d%2FrRddXw1CDGc%2F
    </guid>
    </item>
    <item>
    <title>Medgar Evers: Assassin's gun forever changed a family</title>
    <link>
    http://apidata.usatoday.com/story/news/2013/06/02/medgar-evers-family-legacy/2378631/?kjnd=BaA5upNtQ2K1p9p5GPVNGw15i1RtOV5fb0YlmY%2BqlVnJuvQv0KU7V0H66KhHzJ4J-b563b6d3-0383-4f78-bb8b-201a924382d5_qD0NjBVJM7sRd3kgIUvC5rCRiQmjXqfExiwgXK9XTkYpydLd%2FzCVzcbkfK4DaGDk
    </link>
    <pubDate>Sun, 2 Jun 2013 12:00:06 GMT</pubDate>
    <description>After his death, his widow and his brother each forged civil rights legacies of their own.</description>
    <guid isPermalink="true">
    http://apidata.usatoday.com/story/news/2013/06/02/medgar-evers-family-legacy/2378631/?kjnd=GmynM2cG%2Batyi2gP1MA0W7u3Cs9zcm9VXzoBVY1uPAGPFXAlVnrXhGWi5zOjBZR1-b563b6d3-0383-4f78-bb8b-201a924382d5_UR9C9XUF5%2BfunbQyjDy4a%2FcDOPtW3GU9pRHx%2FRbsPJ%2F4qP4JT0Dx0Nw3z50CGajm
    </guid>
    </item>
    </channel>
    </rss>

    I make the call using this code:

          List<element> el = new List<element>();
                string result = "";
                XDocument doc2;
                String final = "http://api.usatoday.com/open/articles/topnews?api_key=API_KEY";
                HttpResponseMessage response = await httpclient.GetAsync(final);
                result = await response.Content.ReadAsStringAsync();
                result = result.Replace("<br>", Environment.NewLine);
    
    
                doc2 = XDocument.Load(await response.Content.ReadAsStreamAsync());
    
               //===================================================================================================================================
                try
                {
                    el = (from q in doc2.Descendants("{cf:}item")
                          select new element
                          {
                              Tittle = q.Element("{cf:}title").Value,
                              Link = q.Element("{cf:}link").Value,
                              description = q.Element("{cf:}description").Value,
                              pubDate = q.Element("{cf:}pubDate").Value
    
                          }).ToList<element>();
    
    
                }
                catch (Exception)
                {
                }
                //===============================================================================================
                if (el.Count != 0)
                {
                    readButton.Content = "Data Available";
    
                }
                else if (el.Count == 0)
                {
                    readButton.Content = "Data NOT  Available";
                }

    But this doesn't work. Every time i make the call i get 0 items in List el.

    Sunday, June 2, 2013 4:30 PM

Answers

  • Those elements being part of the blank namespace, use:

                    el = (from q in doc2.Descendants("item")
                          select new Element
                          {
                              Title = q.Element("title").Value,
                              Link = q.Element("link").Value,
                              Description = q.Element("description").Value,
                              PubDate = q.Element("pubDate").Value
                          }).ToList<Element>();

     


    • Edited by ForInfo Monday, June 3, 2013 2:30 PM
    • Proposed as answer by jrboddie Monday, June 3, 2013 2:39 PM
    • Marked as answer by kalel111 Thursday, June 6, 2013 9:41 PM
    Monday, June 3, 2013 2:30 PM
  • I agree with ForInfo.  I just parsed your sample feed successfully with the change suggested above.
    • Marked as answer by kalel111 Thursday, June 6, 2013 9:41 PM
    Monday, June 3, 2013 2:39 PM

All replies

  • Option 1:

    You can refer to Syndication sample @ code.msdn.com that will help you with both RSS and Atom Feeds. With this you do not need to parse XML

    Option 2:

    Further if you still want XML way then see Windows Runtime XML data API sample 

    Option 3:

    And not to further confuse you but there is something called LINQ to XML, for that you may refer Get element values from XML in C#


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!



    Sunday, June 2, 2013 4:57 PM
  • Thanks for answering but i do not want to use a different method. I am already doing option 3.

    I think the way i parse the data is the problem. Can someone else see what the problem is?

    Monday, June 3, 2013 1:50 PM
  • Those elements being part of the blank namespace, use:

                    el = (from q in doc2.Descendants("item")
                          select new Element
                          {
                              Title = q.Element("title").Value,
                              Link = q.Element("link").Value,
                              Description = q.Element("description").Value,
                              PubDate = q.Element("pubDate").Value
                          }).ToList<Element>();

     


    • Edited by ForInfo Monday, June 3, 2013 2:30 PM
    • Proposed as answer by jrboddie Monday, June 3, 2013 2:39 PM
    • Marked as answer by kalel111 Thursday, June 6, 2013 9:41 PM
    Monday, June 3, 2013 2:30 PM
  • I agree with ForInfo.  I just parsed your sample feed successfully with the change suggested above.
    • Marked as answer by kalel111 Thursday, June 6, 2013 9:41 PM
    Monday, June 3, 2013 2:39 PM