Solved

Parse XML in C# / Wordpress

Posted on 2014-02-04
2
1,263 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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, …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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