我正在尝试阅读Word Press博客中的rss2提要。我已经找到了这样的例子,包括我在这里为我的代码改编的例子。问题是它不能读取content:encoded标签。我搜索并找到了其他有此问题的人,但似乎没有任何问题得到解决。
我在一个带有C#代码的asp.net页面上使用中继器来显示数据。
问题似乎是content:encoded标签没有在http://purl.org/dc/elements/1.0/modules/content/中定义,我已经检查过了,它根本没有在那里列出。当我设置断点并检查值时,"content“实际上是获取另一个标记的值:"{0}”是第一个post读入post对象后的值。尽管它从未在中继器中显示。
我假设内容一定是一次性在该URL上定义的,因为这应该是一次性工作的。但现在不是了。
除了将XML作为字符串读取并重新发明轮子来读取标记之外,有没有一种已知的方法来解决这个问题?有没有我可以提供的链接来定义代码使用的内容标签?(我假设这就是URLS的目的)。我可以创建我自己的带有定义的页面吗?
下面是后面的代码:
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");
}
}
}下面是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>发布于 2014-05-01 00:32:09
我找到了正确的名称空间(如果这是用于此的正确术语。在那里我有:
http://purl.org/dc/elements/1.0/modules/content/我将其替换为:
http://purl.org/rss/1.0/modules/content/现在它起作用了。
https://stackoverflow.com/questions/23391592
复制相似问题