在 C# 中寻找 razor 语法的简单 RSS 阅读器时,我遇到了这个网站: http: //our.umbraco.org/forum/developers/razor/27409-Consume-an-RSS-feed-in-Razor。
正如它在代码中的注释中所说,它可能是 URL 是硬编码的。我很好奇它究竟是如何硬编码到一个 RSS 提要的,看代码似乎并不明显。例如,如果我尝试替换“http://tdsb.on.ca/RSS/MediaRoom.xml”的 url,它就会变成空白。
@using System.Xml;
@{
//Get the XML from remote URL
XmlDocument xml = new XmlDocument();
**//URL currently hardcoded - but you could use a macro param to pass in URL**
xml.Load("http://blog.orcare.com/rss");
//Select the nodes we want to loop through
XmlNodeList nodes = xml.SelectNodes("//item");
//Traverse the entire XML nodes.
foreach (XmlNode node in nodes)
{
//Get the value from the <title> node
var title = node.SelectSingleNode("title").InnerText;
//Get the value from the <description> node
var description = node.SelectSingleNode("description").InnerText;
<h1>@title</h1>
@Html.Raw(description)
}
}