Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemap generates xml with utf-16 encoding. #36

Open
Lemonadegit opened this issue Nov 28, 2014 · 0 comments
Open

Sitemap generates xml with utf-16 encoding. #36

Lemonadegit opened this issue Nov 28, 2014 · 0 comments

Comments

@Lemonadegit
Copy link

The Sitemap genereator may still generate a sitemap with utf-16 encoding specified in the xml declaration because it uses XmlTextWriter to generate the xml which retrieves the encoding from the environment settings and will overwrite the content encoding declared in the XDocument.

I had to create a custom ActionResult using StringWriter rather than XmlTextWriter:

    public class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding
        {
            get { return new UTF8Encoding(false); }
        }
    }

    public class Utf8SitemapResult : ActionResult
    {
        private readonly IEnumerable<ISitemapItem> items;
        private readonly ISitemapGenerator generator;

        public Utf8SitemapResult(IEnumerable<ISitemapItem> items) : this(items, new SitemapGenerator())
        {

        }

        public Utf8SitemapResult(IEnumerable<ISitemapItem> items, ISitemapGenerator generator)
        {
            Ensure.Argument.NotNull(items, "items");
            Ensure.Argument.NotNull(generator, "generator");

            this.items = items;
            this.generator = generator;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            response.ContentType = "text/xml";
            response.ContentEncoding = Encoding.UTF8;

            using (var writer = new Utf8StringWriter())
            {
                var doc = generator.GenerateSiteMap(items);
                doc.Save(writer, SaveOptions.None);

                response.Write(writer);
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant