我正在尝试解决一个错误,我在http://captainobvio.us生成的所有RSS源在Internet Explorer(版本8和9)中产生以下错误:
Feed代码错误从当前编码切换到不支持的指定编码.行:1个字符:40
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-16"?>
问题是通过HTTP标头发送的实际编码类型与文档声明的不同.以下是将代码输出写入HTML的代码:
public ContentResult Index()
{
var feed = _syndication.SyndicateIdeas(_repository.GetIdeas(0,15).Ideas);
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb, new XmlWriterSettings { Encoding = Encoding.UTF8, NewLineHandling = NewLineHandling.Entitize, NewLineOnAttributes = true, Indent = true}))
{
feed.SaveAsRss20(writer);
writer.Close();
}
return Content(sb.ToString(), "application/rss+xml", Encoding.UTF8);
}
Run Code Online (Sandbox Code Playgroud)
以下是我使用.NET 4.0中的System.ServiceModel.Syndication实际构建feed的代码:
var feed = new SyndicationFeed("CaptainObvio.us - Recent Ideas",
"The most recent ideas posted by the Community on CaptainObvio.us", new Uri("http://captainobvio.us/"), "CaptainObvio.us", new DateTimeOffset(ideas[0].DatePosted), items)
{
Generator = …Run Code Online (Sandbox Code Playgroud)