Last day, I was generating XML content as response for the sitemap link. The link points to a cshtml page whose response content-type is 'text/xml'. And, all the xml contents are rendered as raw content in the page using Razor syntax. The code works fine and generated required XML content but showed error when I tried to preview the XML contents in browser. I started getting error - "error on line 3 at column 6: XML declaration allowed only at the start of the document" (as shown below).
I switched to view-source of the page and could see the white space rendered to my surprise.
The page and code was looking perfectly fine without any html elements or visible special characters.
A second close look at the code, I figured out the problem. In fact, organising each lines neatly was causing this issue in this case.
All we have to do is to remove the gap between the snippet and the rendered code as shown below.
Hope this helps.