Solved

Parse XML in C# / Wordpress

Posted on 2014-02-04
2
1,163 Views
Last Modified: 2016-12-07
Hello

I want to parse the following xml data:

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title>My CMS</title>
<atom:link href="http://test.com/wordpress/?feed=rss2" rel="self" type="application/rss+xml"/>
<link>http://test.com/wordpress</link>
<description>Just another WordPress site</description>
<lastBuildDate>Thu, 30 Jan 2014 19:01:12 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.8.1</generator>
<item>
<title>sdaffs fs fs fsdfsd</title>
<link>http://test.com/wordpress/?p=11</link>
<comments>http://test.com/wordpress/?p=11#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:01:12 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<category>
<![CDATA[ James ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=11</guid>
<description>
<![CDATA[ saf fsdfs f ]]>
</description>
<content:encoded>
<![CDATA[ <p>saf fsdfs f</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=11</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>fa fs afs af</title>
<link>http://test.com/wordpress/?p=9</link>
<comments>http://test.com/wordpress/?p=9#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:00:47 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=9</guid>
<description>
<![CDATA[ asf as fsf sfd s ]]>
</description>
<content:encoded>
<![CDATA[ <p>asf as fsf sfd s</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=9</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>dfdfs fa fas sdfasf</title>
<link>http://test.com/wordpress/?p=7</link>
<comments>http://test.com/wordpress/?p=7#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:00:30 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ James ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=7</guid>
<description>
<![CDATA[ sdf f asf sf sf sd ]]>
</description>
<content:encoded>
<![CDATA[ <p>sdf f asf sf sf sd</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=7</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>sd afs fa fa</title>
<link>http://test.com/wordpress/?p=5</link>
<comments>http://test.com/wordpress/?p=5#comments</comments>
<pubDate>Thu, 30 Jan 2014 18:59:51 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=5</guid>
<description>
<![CDATA[ s af af dfa ]]>
</description>
<content:encoded>
<![CDATA[ <p>s af af dfa</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=5</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>Hello world!</title>
<link>http://test.com/wordpress/?p=1</link>
<comments>http://test.com/wordpress/?p=1#comments</comments>
<pubDate>Thu, 30 Jan 2014 08:28:59 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<guid isPermaLink="false">http://localhost/wordpress-3.8/wordpress/?p=1</guid>
<description>
<![CDATA[
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
]]>
</description>
<content:encoded>
<![CDATA[
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=1</wfw:commentRss>
<slash:comments>1</slash:comments>
</item>
</channel>
</rss>

Open in new window


So it's a Wordpress RSS with several Items.

I am using this piece of code to parse it:

namespace WordPressRSSDLL
{
    public class News
    {
        public static IList<FeedViewModel> GetBlogFeed(string feedUrl, int feedCount)
        {
            XNamespace content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/"
                );
            var doc = XDocument.Load(feedUrl);
            var feeds = doc.Descendants("item").Select(x =>
            new FeedViewModel
            {
                Title = x.Element("title").Value,
                PublishedDate = DateTime.Parse(x.Element("pubDate").Value),
                Url = x.Element("link").Value,
               Content = x.Element(content + "encoded").Value
            })
            .OrderByDescending(x => x.PublishedDate)
            .Take(feedCount);
            return feeds.ToList();
        }
    }
    public class FeedViewModel
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public string Content { get; set; }
        public DateTime PublishedDate { get; set; }
        public List<string> Categories { get; set; }
    }
  
}

Open in new window


Which is working quite good for the strings/Datetime. But how can I parse multiple Categories in an item? I could just parse one category, that's working, but I want a list of all categories per item...

How can I do that?

Thanks

James
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
<channel>
<title>My CMS</title>
<atom:link href="http://test.com/wordpress/?feed=rss2" rel="self" type="application/rss+xml"/>
<link>http://test.com/wordpress</link>
<description>Just another WordPress site</description>
<lastBuildDate>Thu, 30 Jan 2014 19:01:12 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.8.1</generator>
<item>
<title>sdaffs fs fs fsdfsd</title>
<link>http://test.com/wordpress/?p=11</link>
<comments>http://test.com/wordpress/?p=11#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:01:12 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<category>
<![CDATA[ James ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=11</guid>
<description>
<![CDATA[ saf fsdfs f ]]>
</description>
<content:encoded>
<![CDATA[ <p>saf fsdfs f</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=11</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>fa fs afs af</title>
<link>http://test.com/wordpress/?p=9</link>
<comments>http://test.com/wordpress/?p=9#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:00:47 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=9</guid>
<description>
<![CDATA[ asf as fsf sfd s ]]>
</description>
<content:encoded>
<![CDATA[ <p>asf as fsf sfd s</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=9</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>dfdfs fa fas sdfasf</title>
<link>http://test.com/wordpress/?p=7</link>
<comments>http://test.com/wordpress/?p=7#comments</comments>
<pubDate>Thu, 30 Jan 2014 19:00:30 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ James ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=7</guid>
<description>
<![CDATA[ sdf f asf sf sf sd ]]>
</description>
<content:encoded>
<![CDATA[ <p>sdf f asf sf sf sd</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=7</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>sd afs fa fa</title>
<link>http://test.com/wordpress/?p=5</link>
<comments>http://test.com/wordpress/?p=5#comments</comments>
<pubDate>Thu, 30 Jan 2014 18:59:51 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Hello ]]>
</category>
<guid isPermaLink="false">http://test.com/wordpress/?p=5</guid>
<description>
<![CDATA[ s af af dfa ]]>
</description>
<content:encoded>
<![CDATA[ <p>s af af dfa</p> ]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=5</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item>
<title>Hello world!</title>
<link>http://test.com/wordpress/?p=1</link>
<comments>http://test.com/wordpress/?p=1#comments</comments>
<pubDate>Thu, 30 Jan 2014 08:28:59 +0000</pubDate>
<dc:creator>
<![CDATA[ admin ]]>
</dc:creator>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<guid isPermaLink="false">http://localhost/wordpress-3.8/wordpress/?p=1</guid>
<description>
<![CDATA[
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
]]>
</description>
<content:encoded>
<![CDATA[
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
]]>
</content:encoded>
<wfw:commentRss>http://test.com/wordpress/?feed=rss2&p=1</wfw:commentRss>
<slash:comments>1</slash:comments>
</item>
</channel>
</rss>

Open in new window

0
Comment
Question by:Ruttensoft
2 Comments
 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
ID: 39834975
This is not a direct answer to your question but I think this is a better/easier solution:

If it is regarding a RSS or ATOM feed, you can use the SyndicationFeed class to convert it. This is a class specially for RSS and ATOM feeds.

You can do like the following to load the xml from an URL:
var reader = XmlReader.Create(rssFeedUrl);
    var feed = SyndicationFeed.Load(reader);

Open in new window


From there you can iterate through the "Items" property and get everything you need.
0
 

Author Comment

by:Ruttensoft
ID: 39834996
Hey that's it, thanks!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question