为视图读入XML的简单方法(RSS提要)
我以前只做过一些小的XML解析/阅读,但从未在MVC中做过。我只想抓取一个小片段,但我不知道该用什么来显示这个RSS提要 例如,如果XML如下所示为视图读入XML的简单方法(RSS提要),xml,asp.net-mvc,rss,linq-to-xml,viewbag,Xml,Asp.net Mvc,Rss,Linq To Xml,Viewbag,我以前只做过一些小的XML解析/阅读,但从未在MVC中做过。我只想抓取一个小片段,但我不知道该用什么来显示这个RSS提要 例如,如果XML如下所示 <title>Most Recent News</title> <link>http://na.leagueoflegends.com/en/rss.xml</link> <description></description>
<title>Most Recent News</title>
<link>http://na.leagueoflegends.com/en/rss.xml</link>
<description></description>
<language>en-US</language>
<item>
<title>Match History Beta Now Live</title>
<link>http://na.leagueoflegends.com/en/news/game-updates/features/announcing-new-match-history-beta?ref=rss</link>
<description>lots of long text</description>
<pubDate>Wed, 04 Jun 2014 14:02:56 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">17129 at http://na.leagueoflegends.com</guid>
</item>
<item>
<title>Riot with us at live events!</title>
<link>http://na.leagueoflegends.com/en/news/community/community-events/riot-us-live-events?ref=rss</link>
<description>Lots of lon</description>
<pubDate>Mon, 30 Jun 2014 18:27:13 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">18266 at http://na.leagueoflegends.com</guid>
</item>
....dozens more....
我一直在乱搞一些随机的东西,比如XDocument
,我可以存储第一个项目,但没有其他东西
XDocument xmlFile = XDocument.Load(@"http://na.leagueoflegends.com/en/rss.xml");
foreach (XElement element in xmlFile.Descendants("title"))
{
String words = element.ToString();
ViewBag.Ltitle = Regex.Replace(words, "<.*?>", string.Empty);
}
foreach (XElement element in xmlFile.Descendants("link"))
{
String words = element.ToString();
ViewBag.Llink = Regex.Replace(words, "<.*?>", string.Empty);
}
foreach (XElement element in xmlFile.Descendants("description"))
{
String words = element.ToString();
ViewBag.Ldescription = Regex.Replace(words, "<.*?>", string.Empty);
}
foreach (XElement element in xmlFile.Descendants("pubDate"))
{
String words = element.ToString();
ViewBag.LDate = Regex.Replace(words, "<.*?>", string.Empty);
}
XDocument xmlFile=XDocument.Load(@)http://na.leagueoflegends.com/en/rss.xml");
foreach(xmlFile.substands(“title”)中的XElement元素)
{
String words=element.ToString();
ViewBag.Ltitle=Regex.Replace(words,“,string.Empty);
}
foreach(xmlFile.substands(“link”)中的XElement元素)
{
String words=element.ToString();
ViewBag.Llink=Regex.Replace(words,“,string.Empty);
}
foreach(xmlFile.description(“description”)中的XElement元素)
{
String words=element.ToString();
ViewBag.Ldescription=Regex.Replace(words,“,string.Empty);
}
foreach(xmlFile.subscriptions(“pubDate”)中的XElement元素)
{
String words=element.ToString();
ViewBag.LDate=Regex.Replace(words,“,string.Empty);
}
获取数据并对其进行解析,但我不确定如何保存多个项目。我只想为最近发布的三篇新闻文章提供ViewBag数据,然后在需要的地方给他们打电话
我想做的事情的例子
@ViewBag.Ltitle_1<br />
<a href="@ViewBag.Llink_1">@ViewBag.Ldescription_1</a><br />
@ViewBag.LDate_1
@ViewBag.Ltitle_2<br />
<a href="@ViewBag.Llink_2">@ViewBag.Ldescription_2</a><br />
@ViewBag.LDate_2
@ViewBag.Ltitle_3<br />
<a href="@ViewBag.Llink_3">@ViewBag.Ldescription_3</a><br />
@ViewBag.LDate_3
@ViewBag.Ltitle_1
@ViewBag.LDate_1
@ViewBag.Ltitle_2
@ViewBag.LDate_2
@ViewBag.Ltitle_3
@ViewBag.LDate_3
有关参考信息,当前RSS的链接为:
谢谢大家! 想出了一个解决方案,以防其他人需要帮助 控制器
XDocument xmlFile = XDocument.Load(@"http://na.leagueoflegends.com/en/rss.xml");
var LoLtitles = from service in xmlFile.Descendants("item")
select (string)service.Element("title");
var LoLlinks = from service in xmlFile.Descendants("item")
select (string)service.Element("link");
var LoLdescriptions = from service in xmlFile.Descendants("item")
select (string)service.Element("description");
var LoLDates = from service in xmlFile.Descendants("item")
select (string)service.Element("pubDate");
ViewBag.titles = LoLtitles.ToArray();
ViewBag.links = LoLlinks.ToArray();
ViewBag.descriptions = LoLdescriptions.ToArray();
ViewBag.dates = LoLDates.ToArray();
查看
@ViewBag.titles[0]<br />
<a href="@ViewBag.links[0]">@ViewBag.descriptions[0]</a><br />
@ViewBag.dates[0]<br />
@ViewBag.titles[1]<br />
<a href="@ViewBag.links[1]">@ViewBag.descriptions[1]</a><br />
@ViewBag.dates[1]<br />
@ViewBag.titles[2]<br />
<a href="@ViewBag.links[2]">@ViewBag.descriptions[2]</a><br />
@ViewBag.dates[2]<br />
@ViewBag.titles[0]
@查看包日期[0]
@ViewBag.titles[1]
@ViewBag.dates[1]
@ViewBag.titles[2]
@ViewBag.dates[2]
@ViewBag.titles[0]<br />
<a href="@ViewBag.links[0]">@ViewBag.descriptions[0]</a><br />
@ViewBag.dates[0]<br />
@ViewBag.titles[1]<br />
<a href="@ViewBag.links[1]">@ViewBag.descriptions[1]</a><br />
@ViewBag.dates[1]<br />
@ViewBag.titles[2]<br />
<a href="@ViewBag.links[2]">@ViewBag.descriptions[2]</a><br />
@ViewBag.dates[2]<br />