首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET ServiceModel.Syndication - RSS源上的更改编码

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

Stack Overflow用户
提问于 2011-03-27 22:19:45
回答 1查看 2K关注 0票数 7

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

不支持从当前编码切换到指定编码的

源代码错误切换。行:1字符: 40

问题是,通过HTTP报头发送的实际编码类型与文档声明的不同。下面是我的代码,用于将提要的输出写入HTML:

代码语言:javascript
复制
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);
        } 

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

代码语言:javascript
复制
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。我还检查了编码命名空间,以确定是否有UTF16选项(这样我就可以更正header而不是XML ),但是找不到。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-28 06:30:50

之所以会发生这种情况,是因为您要将一个StringBuilder传递给XmlWriter构造函数。.NET中的字符串是unicode,因此XmlWriter假设utf-16,您不能修改它。

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
        }
    }
}

在控制器操作中,只需返回此结果,这样就不会将控制器操作与管道代码混淆:

代码语言:javascript
复制
public ActionResult Index()
{
    var ideas = _repository.GetIdeas(0, 15).Ideas;
    var feed = _syndication.SyndicateIdeas(ideas);
    return new SyndicationResult(feed);
} 
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5452878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档