Extend rss feed and read items from SyndicationFeed for Win8 app

1k views Asked by At

I've extended a RSS feed with custom fielditems, eg, with an image url (http://linktoimage

I'm reading the RSS feed with:

    SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri(feedUrl));

But in order to read the new element I need to extend the SyndicationFeed with the items (it takes one a handfull of default RSS items). I couldn't find a recent example of how to do this. Most code available could not be aplied to a Win8 app. Closest I found was:

    i.ElementExtensions.First(element => element.NodeName == "imgurl").NodeValue); 

But this this caused an exception error. When looked at the value of the element it show's me the published tag instead of the value. I expected it to find the first imgurl tag and return the value of it. Just as it does in the code where i found the code ( http://code.msdn.microsoft.com/windowsapps/XAML-Twitter-Client-e343d336 )

How can I read a nd the extra xml tags i've added to the feed (as a string) when using "SyndidationFeed"?

1

There are 1 answers

1
danielrozo On

The best way of doing this is using LINQ. For example, reading a WordPress RSS (pay attention to the commments):

XmlDocument xDoc = await XmlDocument.LoadFromUriAsync(new Uri(blog.URL)); //URL you're trying to read
        StringReader stringReader= new StringReader(xDoc.GetXml());
        XmlReader xmlReader = XmlReader.Create(stringReader);
        XDocument loadedPosts = XDocument.Load(xmlReader); //this can be done simpler using HttpClient.GetStringAsync
        XNamespace dc = "http://purl.org/dc/elements/1.1/";
        XNamespace content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/"); //declare namespaces for dc:content
        var data = from query in loadedPosts.Descendants("item") //gets all the "item" tags
                   select new Post //class you must create
                   {
                       NombreBlog = (string)query.Parent.Element("title"), //then you simply change 'Element("title")' with 'Element("propertyYouWant")'
                       Titulo = (string)query.Element("title"),
                       Autor = (string)query.Element(dc + "creator"),
                       Contenido = (string)query.Element(content + "encoded"),
                       PubDate = (string)query.Element("pubDate"),
                       Link = (string)query.Element("link"),
                       ID = getId((string)query.Element("guid")),
                       Imagen = getImage((string)query.Element(content + "encoded"))
                   };

This way you can get all the attributes or tags you want, you can even asign functions to them passing a tag and returning only an image source, for example.