1

Please, see the following image: enter image description here

In English:

XML Parsing Error: XML or text declaration not at start of entity. Location: https:// ****.com/sitemap Line Number 4, Column 1:

I can't properly create a sitemap, because I'm always stuck on that error. I don't know why but it seems like XmlWriter is adding exactly 3 blank lines before my xml tag. I've tried playing with XmlWriterSettings and lots of different things at no avail.

This is part of my code:

public override void ExecuteResult(ControllerContext context)
{
    context.HttpContext.Response.ContentType = "text/xml;";
    context.HttpContext.Response.ContentEncoding = Encoding.UTF8;

    using (XmlWriter writer = new XmlTextWriter(context.HttpContext.Response.Output))
    {
        writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
...
        writer.WriteEndElement();

        writer.Flush();
        writer.Close();
    }
}

Does anyone have any idea on how to solve this? Thanks!

eestein
  • 4,914
  • 8
  • 54
  • 93
  • @ThomasWeller You're saying I should remove `charset=UTF-8` from the header? – eestein Jun 29 '15 at 20:09
  • Standard approach is to return [FileResult](https://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult(v=vs.118).aspx)/FileStreamResult instead of writing your own - http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc ... Not exactly what you asking, but likely what you actually trying to achieve. – Alexei Levenkov Jun 29 '15 at 20:20
  • @AlexeiLevenkov thanks for the link, but this is way is how people make their sitemap.xml available using asp.net mvc. This is just one example: https://github.com/benfoster/Fabrik.Common/blob/master/src/Fabrik.Common.Web/ActionResults/SitemapResult.cs but you can find others. Anyway, thanks for your comment. – eestein Jun 29 '15 at 20:22

1 Answers1

1

The UTF-8 byte order mark (BOM) is counted as newlines. Remove the BOM to get rid of the 3 bytes EF BB BF by using new UTF8Encoding(false):

using (XmlWriter writer = XmlWriter.Create(memoryStream, 
new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(false)}))

Alternatively, a XmlTextWriter works fine for me as well:

using (XmlWriter writer = new XmlTextWriter(memoryStream, new UTF8Encoding(false)))
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Thomas, thank you for the update, here's what I got so far, see if you can help me out. There are no constructors for TextWriter and UTF8Encoding class so I changed ContentEncoding from the TextWriter and now the xml header disappeared. Was that the expected behavior? – eestein Jun 29 '15 at 20:34
  • Sorry, i'm confused. TextWriter or XmlTextWriter? What is in your using-statement now? Also, let's clean up the comments a bit. – Thomas Weller Jun 29 '15 at 20:47
  • If my answer didn't solve the problem, you can also write your own answer and define what you have finally used. – Thomas Weller Jun 29 '15 at 20:48
  • Nah, you did help. I appreciate your time. I'm just fixing some nodes with invalid characters, hopefully that would do. I'm already seeing a page I wasn't that's why I accepted the answer and I'm assuming it'll work now as soon as fix those nodes :) Thanks again. PS: I updated my question with my current code. – eestein Jun 29 '15 at 20:59