// action to return the feed
[Route("site/GetRssFeed/{type}")]
public IActionResult GetRssFeed(ArticleStatusTypes type)
{
var feed = _rss.BuildXmlFeed(type);
return Content(feed, "text/xml");
}
public string BuildXmlFeed(ArticleStatusTypes type)
{
var key = $"RssFeed{Convert.ToInt32(type)}{_appInfo.ApplicationId}";
var articles =
_cache.GetCachedData(key) ??
_cache.SetCache(key, _service.GetItems(Convert.ToInt32(type), _appInfo.CacheCount));
StringWriter parent = new StringWriter();
using (XmlTextWriter writer = new XmlTextWriter(parent))
{
writer.WriteProcessingInstruction("xml-stylesheet", "title=\"XSL_formatting\" type=\"text/xsl\" href=\"/skins/default/controls/rss.xsl\"");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
// write out
writer.WriteStartElement("channel");
// write out -level elements
writer.WriteElementString("title", $"{_appInfo.ApplicationName} {type}");
writer.WriteElementString("link", _appInfo.WebsiteUrl);
//writer.WriteElementString("description", Description);
writer.WriteElementString("ttl", "60");
writer.WriteStartElement("atom:link");
//writer.WriteAttributeString("href", Link + Request.RawUrl.ToString());
writer.WriteAttributeString("rel", "self");
writer.WriteAttributeString("type", "application/rss+xml");
writer.WriteEndElement();
if (articles != null)
{
foreach (var article in articles)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", article.Title);
writer.WriteElementString("link", _appInfo.WebsiteUrl); // todo build article path
writer.WriteElementString("description", article.Summary);
writer.WriteEndElement();
}
}
// write out
writer.WriteEndElement();
// write out
writer.WriteEndElement();
}
return parent.ToString();
}
我想你會只使用XmlWriter並將Xml寫入響應。關鍵是確保你創建一個正確的結構並使用正確的名稱和命名空間。 – Pawel