使用 C#/MVC 2 解析 RSS 1.0
首先,我不敢相信2011年还有人还在使用RSS 1.0,猜猜你真的学到了新东西。好吧,我正在尝试解析他们的提要并将内容放在我的客户网站上(我获得了许可,因此这里没有违法)。正如你可以想象的那样,我的第一次尝试惨败了(正在编写 2.0 的代码),所以我回到了绘图板,这就是我的想法。
RssController
public virtual ActionResult Index()
{
List<RssFeedItem> rssList = new List<RssFeedItem>();
XmlDocument doc = new XmlDocument();
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
manager.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlTextReader reader = new XmlTextReader("http://wdfw.wa.gov/news/newsrss.php");
doc.Load(reader);
XmlNodeList nodes = doc.SelectNodes("/rdf:RDF//rss:item",manager);
foreach(XmlNode node in nodes)
{
XmlNodeList aboutNode = node.SelectNodes("rss:about", manager);
XmlNodeList titleNode = node.SelectNodes("rss:title", manager);
XmlNodeList linkNode = node.SelectNodes("rss:link", manager);
var item = new RssFeedItem
{
Link = linkNode.Count == 0 ? "" : linkNode[0].InnerText,
Title = titleNode.Count == 0 ? "" : titleNode[0].InnerText,
About = aboutNode.Count == 0 ? "" : aboutNode[0].InnerText
};
rssList.Add(item);
}
return View(rssList);
}
我正在尝试利用部分视图 (ascs) 来执行此操作,看起来像这样
Index.aspx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<GodsCreationTaxidermy.Models.RssFeedItem>" %>
<%
foreach (RssFeedItem item in (IEnumerable<RssFeedItem>)ViewData.Model)
{
Response.Write(string.Format("<div id={0}><a href={1} target=\"_blank\" /> <strong>{2}</strong></div>",
Model.About.Truncate(5), Model.Link, Model.Title.Truncate(25)));
} %>
然后我在 mySite.Master 中尝试 RenderPartial
<% Html.RenderPartial("Index");%>
问题是,网站甚至从不加载,甚至给出某种错误,它只是坐在那儿,FF 的小加载图标在旋转。
编辑
好吧,我对之前的代码做了一些重大更改,结果相同,没有错误,甚至不加载网站(只是坐着和旋转)。这是来自控制器的代码
[MoveFormsScript]
[CanonicalUrlAttribute("Rss")]
public virtual ActionResult Index()
{
return View(new RssList());
}
现在是来自 RssList 的代码
[UIHint("Feeds")]
public SelectList Feeds { get; private set; }
[Required(ErrorMessage = "Rss Feeduired")]
public string Feed { get; set; }
public RssList()
{
this.Feeds = GetRssFeed(null);
}
public SelectList GetRssFeed(string selectedValue)
{
List<RssFeedItem> rssList = new List<RssFeedItem>();
XmlDocument doc = new XmlDocument();
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("rdf", "http://www.w3.org/1 999/02/22-rdf-syntax-ns#");
manager.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlTextReader reader = new XmlTextReader("http://wdfw.wa.gov/news/newsrss.php");
doc.Load(reader);
XmlNodeList nodes = doc.SelectNodes("/rdf:RDF//rss:item", manager);
foreach (XmlNode node in nodes)
{
XmlNodeList aboutNode = node.SelectNodes("rss:about", manager);
XmlNodeList titleNode = node.SelectNodes("rss:title", manager);
XmlNodeList linkNode = node.SelectNodes("rss:link", manager);
var item = new RssFeedItem
{
Link = linkNode.Count == 0 ? "" : linkNode[0].InnerText,
Title = titleNode.Count == 0 ? "" : titleNode[0].InnerText,
About = aboutNode.Count == 0 ? "" : aboutNode[0].InnerText
};
rssList.Add(item);
}
return new SelectList(rssList, "Link", "Title", selectedValue);
}
最后是我的部分视图
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<GodsCreationTaxidermy.Helpers.BusinessDTO.RssList>" %>
<%
foreach (var item in (IEnumerable<RssList>)ViewData.Model)
{
Response.Write(string.Format("<div id=\"{0}\"><a href=\"{1}\" target=\"_blank\" /> <strong>{2}</strong></div>",
item.Feed[0].ToString().Truncate(10), item.Feed[1], item.Feed[2].ToString().Truncate(100)));
} %>
谁能帮助我?
First, I cant believe that anyone in 2011 is still using RSS 1.0, guess you really learn something new. Well I'm trying to parse their feed and put the content on my clients site (I got permissions so no law breaking here). As you can imagine my first attempt failed miserably (was wrting code for 2.0) so I went back to the drawing board and here's what I've come up with.
RssController
public virtual ActionResult Index()
{
List<RssFeedItem> rssList = new List<RssFeedItem>();
XmlDocument doc = new XmlDocument();
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
manager.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlTextReader reader = new XmlTextReader("http://wdfw.wa.gov/news/newsrss.php");
doc.Load(reader);
XmlNodeList nodes = doc.SelectNodes("/rdf:RDF//rss:item",manager);
foreach(XmlNode node in nodes)
{
XmlNodeList aboutNode = node.SelectNodes("rss:about", manager);
XmlNodeList titleNode = node.SelectNodes("rss:title", manager);
XmlNodeList linkNode = node.SelectNodes("rss:link", manager);
var item = new RssFeedItem
{
Link = linkNode.Count == 0 ? "" : linkNode[0].InnerText,
Title = titleNode.Count == 0 ? "" : titleNode[0].InnerText,
About = aboutNode.Count == 0 ? "" : aboutNode[0].InnerText
};
rssList.Add(item);
}
return View(rssList);
}
I'm attempting to do this utilizing a partial view (ascs) and that looks like this
Index.aspx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<GodsCreationTaxidermy.Models.RssFeedItem>" %>
<%
foreach (RssFeedItem item in (IEnumerable<RssFeedItem>)ViewData.Model)
{
Response.Write(string.Format("<div id={0}><a href={1} target=\"_blank\" /> <strong>{2}</strong></div>",
Model.About.Truncate(5), Model.Link, Model.Title.Truncate(25)));
} %>
Then I try RenderPartial in mySite.Master
<% Html.RenderPartial("Index");%>
The problem is that the site never even loads, or even gives some kind of error, it just sits tere with FF's litte loading icon spinning.
EDIT
Well I've made some major changes to my code from earlier, and same result, no error and it doesnt load the site even (just sits & spins). Here's the code from the Controller
[MoveFormsScript]
[CanonicalUrlAttribute("Rss")]
public virtual ActionResult Index()
{
return View(new RssList());
}
Now the code from RssList
[UIHint("Feeds")]
public SelectList Feeds { get; private set; }
[Required(ErrorMessage = "Rss Feeduired")]
public string Feed { get; set; }
public RssList()
{
this.Feeds = GetRssFeed(null);
}
public SelectList GetRssFeed(string selectedValue)
{
List<RssFeedItem> rssList = new List<RssFeedItem>();
XmlDocument doc = new XmlDocument();
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("rdf", "http://www.w3.org/1 999/02/22-rdf-syntax-ns#");
manager.AddNamespace("rss", "http://purl.org/rss/1.0/");
XmlTextReader reader = new XmlTextReader("http://wdfw.wa.gov/news/newsrss.php");
doc.Load(reader);
XmlNodeList nodes = doc.SelectNodes("/rdf:RDF//rss:item", manager);
foreach (XmlNode node in nodes)
{
XmlNodeList aboutNode = node.SelectNodes("rss:about", manager);
XmlNodeList titleNode = node.SelectNodes("rss:title", manager);
XmlNodeList linkNode = node.SelectNodes("rss:link", manager);
var item = new RssFeedItem
{
Link = linkNode.Count == 0 ? "" : linkNode[0].InnerText,
Title = titleNode.Count == 0 ? "" : titleNode[0].InnerText,
About = aboutNode.Count == 0 ? "" : aboutNode[0].InnerText
};
rssList.Add(item);
}
return new SelectList(rssList, "Link", "Title", selectedValue);
}
And finally my partial view
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<GodsCreationTaxidermy.Helpers.BusinessDTO.RssList>" %>
<%
foreach (var item in (IEnumerable<RssList>)ViewData.Model)
{
Response.Write(string.Format("<div id=\"{0}\"><a href=\"{1}\" target=\"_blank\" /> <strong>{2}</strong></div>",
item.Feed[0].ToString().Truncate(10), item.Feed[1], item.Feed[2].ToString().Truncate(100)));
} %>
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 RssList 类的 XmlNodeList 实例化更改为:
然后将您的 ViewUserControl 更改为:
似乎对我有用。
Change the instantiation of the XmlNodeList is the RssList class to:
Then change your ViewUserControl to this:
Seems to work for me.
我在这里看到一些问题。您的控件需要单个 RssList,控制器正在返回一个选择列表,并且您的 foreach 正在枚举它认为是 IEnumerable的内容。另外,您正在部分视图中进行手动response.writes。如果您不这样做,标记会更清晰。
我们首先从 RssList 类开始。看来您的第一种方法实际上是您想要的,问题只是您在部分视图中所做的事情。
现在,我们返回的是 IEnumerable,而不是 SelectList。如果可能的话,最好不要从代码中操作 UI。那么让我们看看是否需要在控制器上施展魔法。我唯一看到的是,您可以从视图函数中排除“Index”(这是隐含的,因为这是控制器操作的名称。
现在到视图......让我们看看是否可以使其看起来更好
)我认为这应该适合您...如果您遇到任何问题,请告诉我。
There are a few problems I see here. Your control is expecting a single RssList, the Controller is returning a select list, and your foreach is enumerating over what it thinks is an IEnumerable<RssList>. Also, you're doing manual response.writes in the partial view. The markup would be cleaner if you didn't do that.
Let's start with the RssList class first. It looks like your first approach was actually what you wanted, the problem was just what you were doing in the partial view.
Now instead of a SelectList, we're returning an IEnumerable<RssFeedItem>. It's preferable not to manipulate the UI from the code if possible. So let's see if we need to work any magic on the controller. The only thing I see there is that you can exclude "Index" from the View function (it's implied because that is the name of the controller operation.
Now on to the view...let's see if we can make that look better
There I think that should do it for you...let me know if you run into any problems.
我不太熟悉 System.Web.Mvc.SelectList 类,更不用说命名空间了,但我注意到您使用了一个字符串作为 selectedValue 参数。它应该是 RssFeedItem 对象吗?
[编辑]
通过一些 powershell 障碍运行您的 xml 代码部分,这就是我得到的结果。
I'm not really familiar with the System.Web.Mvc.SelectList class much less the namespace, but I noticed your using a string for the selectedValue parameter. Should it be an RssFeedItem object instead?
[edit]
Ran your xml code section through some powershell hurdles and here's what I get.
mod 可以将此提要与此提要合并MVC2 - 使用 RDF 和命名空间使用 RSS 提要 http://www.w3.org/1999/02/22-rdf-syntax-ns#'。我忘了我已经问过了
Can a mod merge this feed with this one MVC2 - Consume RSS feed with RDF and namespace http://www.w3.org/1999/02/22-rdf-syntax-ns#'. I forgot I had already asked
对那些试图帮助我的人做了更多改变。这是控制器代码
然后我创建了一个类(RssList),这是完成大部分工作的地方
RssList
这是 Index.ascx
和正常调用
}
FeedItemCass
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用系统.Web;
Made some more changes dor those who are trying to help me. Here's the controllers code
Then I created a class (RssList), which is where the bulk of the work is done
RssList
This is the Index.ascx
And a normal call
}
FeedItemCass
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;