Solved

Parse XML in C# / Wordpress

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

10 Experts available now in Live!

Get 1:1 Help Now