Solved

Parse XML in C# / Wordpress

Posted on 2014-02-04
2
1,204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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