2

I am trying to read a rss2 feed from a Word Press blog. I have found examples of this, including the one I adapted for my code here. The problem is that it does not read the content:encoded tag. I have searched and found other people that have had this problem, but nothing where it seems to have been solved.

I am using a repeater on an asp.net page with C# code behind to display the data.

The problem seems to be that the content:encoded tag is not defined in http://purl.org/dc/elements/1.0/modules/content/ I've checked it's not listed there at all. When I set a breakpoint and check the value the "content" is actually getting the value of another tag: "{0}" is the value in it after the first post is read into the post object. Although it is never displayed in the repeater.

I assume that content must have been defined at that URL at one time, because this supposedly worked at one time. But it does not now.

Short of reading the XML as a string and re-inventing the wheel to read the tags is there a known way to solve this? Is there a link I can supply that will define the content tag for the code to use? (I am assuming that is the purpose of the URLS). Could I just create my own page with a definition?

Here is the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        // Load the blog posts 
        string sURL1 = "http://www.stellman-greene.com/feed";
        string sURL2 = "http://arnottsuspension.com/?feed=rss2";
        XDocument ourBlog = XDocument.Load(sURL2);


        // Query the <item>s in the XML RSS data and select each one into a new Post()
        IEnumerable<Post> posts =
            from post in ourBlog.Descendants("item")
            select new Post(post);
        postRepeater.DataSource = posts;
        postRepeater.DataBind();




    }
    class Post
    {
        public string Title { get; private set; }
        public DateTime? Date { get; private set; }
        public string Url { get; private set; }
        public string Description { get; private set; }
        public string Creator { get; private set; }
        public string Content { get; private set; }

        private static string GetElementValue(XContainer element, string name)
        {
            if ((element == null) || (element.Element(name) == null))
                return String.Empty;
            return element.Element(name).Value;
        }

        public Post(XContainer post)
        {
            // Get the string properties from the post's element values
            Title = GetElementValue(post, "title");
            Url = GetElementValue(post, "guid");
            Description = GetElementValue(post, "description");
            Creator = GetElementValue(post,
                "{http://purl.org/dc/elements/1.1/}creator");
            Content = GetElementValue(post,
               "{http://purl.org/dc/elements/1.0/modules/content/}encoded");

            // The Date property is a nullable DateTime? -- if the pubDate element
            // can't be parsed into a valid date, the Date property is set to null
            DateTime result;
            if (DateTime.TryParse(GetElementValue(post, "pubDate"), out result))
                Date = (DateTime?)result;
        }

        public override string ToString()
        {
            return String.Format("{0} by {1}", Title ?? "no title", Creator ?? "Unknown");
        }
    }

}

Here is the HTML/ASP:

   <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>


       <asp:Repeater ID="postRepeater" runat="server">
           <ItemTemplate>
               <tr>
                   <td><%# Eval("Title") %></td><br />
               </tr>
               <tr>
                   <td><%# Eval("Description") %></td><br />
               </tr>
               <tr>
                   <td><%# Eval("Content") %></td><br />
               </tr>
               <tr>
                   <td><a href="<%# Eval("URL") %>">Read More</a></td><br /><br />
               </tr>
           </ItemTemplate>


       </asp:Repeater>


    </div>
    </form>
</body>
</html>
valis
  • 235
  • 1
  • 4
  • 15

1 Answers1

3

I found the correct namespace (If that is the correct term to use for this. Where I had:

http://purl.org/dc/elements/1.0/modules/content/

I replaced it with:

http://purl.org/rss/1.0/modules/content/

And it now works.

valis
  • 235
  • 1
  • 4
  • 15