gpt4 book ai didi

.NET ServiceModel.Syndication - 更改 RSS 源的编码

转载 作者:数据小太阳 更新时间:2023-10-29 01:49:23 25 4
gpt4 key购买 nike

我正在尝试解决一个错误,即我在 http://captainobvio.us 生成的所有 RSS 提要在 Internet Explorer(版本 8 和 9)中产生以下错误:

Feed code error Switch from current encoding to specified encoding not supported. Line: 1 Character: 40

<?xml version="1.0" encoding="utf-16"?>

问题是通过 HTTP header 发送的实际编码类型与文档声明的不同。以下是我将提要的输出写入 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);
}

下面是我在 .NET 4.0 中使用 System.ServiceModel.Syndication 实际构建提要的代码:

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 = "CaptainObvio.us - http://captainobvio.us/"
};

return feed;

我想做的是将 XML 文档更改为读取 utf-8 而不是 utf-16。我还检查了 Encoding 命名空间以查看是否有 UTF16 选项(这样我就可以更正 HTTP header 而不是 XML 文档)但没能找到。

是否有一种简单的方法可以直接从 System.ServiceModel.Syndication 更改 XML 文档的编码属性?解决此问题的最简单方法是什么?

最佳答案

发生这种情况的原因是因为您将 StringBuilder 传递给 XmlWriter 构造函数。 .NET 中的字符串是 unicode,因此 XmlWriter 假定为 utf-16,您无法修改它。

因此您可以使用流而不是字符串生成器,然后您可以使用设置控制编码:

var settings = new XmlWriterSettings 
{
Encoding = Encoding.UTF8,
NewLineHandling = NewLineHandling.Entitize,
NewLineOnAttributes = true,
Indent = true
};
using (var stream = new MemoryStream())
using (var writer = XmlWriter.Create(stream, settings))
{
feed.SaveAsRss20(writer);
writer.Flush();
return File(stream.ToArray(), "application/rss+xml; charset=utf-8");
}

综上所述,一个更好、更 MVCish 和我推荐你的解决方案是编写一个 SyndicationResult:

public class SyndicationResult : ActionResult
{
private readonly SyndicationFeed _feed;
public SyndicationResult(SyndicationFeed feed)
{
if (feed == null)
{
throw new HttpException(401, "Not found");
}
_feed = feed;
}

public override void ExecuteResult(ControllerContext context)
{
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
NewLineHandling = NewLineHandling.Entitize,
NewLineOnAttributes = true,
Indent = true
};

var response = context.HttpContext.Response;
response.ContentType = "application/rss+xml; charset=utf-8";
using (var writer = XmlWriter.Create(response.OutputStream, settings))
{
_feed.SaveAsRss20(writer);
}
}
}

并且在您的 Controller 操作中只需返回此结果,这样您就不会将管道代码弄乱您的 Controller 操作:

public ActionResult Index()
{
var ideas = _repository.GetIdeas(0, 15).Ideas;
var feed = _syndication.SyndicateIdeas(ideas);
return new SyndicationResult(feed);
}

关于.NET ServiceModel.Syndication - 更改 RSS 源的编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com