Solved

Parse XML in C# / Wordpress

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now