title: ASP.Net C# MVC5 Razor View XML sitemap
body: <p>My Controller Action looks like this. </p>
  
  <pre><code>public ActionResult Sitemap()
          {
              Response.ContentType = "application/xml";
              return View();
          }
  </code></pre>
  
  <p>My View Sitemap.cshtml looks like this. </p>
  
  <pre><code>@{Layout = null;}&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
  http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"&gt;
      &lt;url&gt;
          &lt;loc&gt;https://www.mywebsitename.com/home&lt;/loc&gt;
          &lt;lastmod&gt;2006-12-12&lt;/lastmod&gt;
          &lt;changefreq&gt;weekly&lt;/changefreq&gt;
          &lt;priority&gt;1.00&lt;/priority&gt;
      &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  
  <p>I am getting following ERROR in browser for page (<code>http://localhost:50831/Sitemap</code>)</p>
  
  <pre><code>This page contains the following errors:
  
  error on line 2 at column 6: XML declaration allowed only at the start of the document
  Below is a rendering of the page up to the first error.
  </code></pre>
  
  <p>I have removed all spaces from View(Sitemap.cshtml). but still error</p>
  
  <p>I even tried <a href="https://stackoverflow.com/questions/19653639/razor-view-extra-line-at-top-of-template">this</a> and inverse the order as shown below but still error.  </p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  @{    Layout = null;}
  &lt;urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
  http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"&gt;
      &lt;url&gt;
          &lt;loc&gt;https://www.mywebsitename.com/home&lt;/loc&gt;
          &lt;lastmod&gt;2006-12-12&lt;/lastmod&gt;
          &lt;changefreq&gt;weekly&lt;/changefreq&gt;
          &lt;priority&gt;1.00&lt;/priority&gt;
      &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  
  <p>It seems like it is sending empty space or line before header which causes above error. 
  Layout is null, view starts with xml tag and from where it is getting empty space. </p>
  
  <p><strong>page source output</strong></p>
  
  <pre><code>----EMPTY LINE----
          &lt;?xml version='1.0' encoding='UTF-8' ?&gt;
          &lt;urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemalocation="http://www.google.com/schemas/sitemap/0.84
          http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"&gt;
              &lt;url&gt;
                  &lt;loc&gt;https://www.mywebsitename.com/home&lt;/loc&gt;
                  &lt;lastmod&gt;2006-12-12&lt;/lastmod&gt;
                  &lt;changefreq&gt;weekly&lt;/changefreq&gt;
                  &lt;priority&gt;1.00&lt;/priority&gt;
              &lt;/url&gt;
          &lt;/urlset&gt;
  </code></pre>
  
  <p>I could not get my head around why? Any Idea?</p>
  
  <p>I checked following but still could not able to find answer</p>
  
  <p><a href="https://stackoverflow.com/questions/13449103/how-to-create-xml-sitemap-programmatically-in-c-sharp">How to create XML sitemap programmatically in c#</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5027959/sitemaps-xml-in-asp-net-mvc">sitemaps.xml in asp.net mvc</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13087696/asp-net-web-sitemap-to-generate-sitemap-xml">ASP.NET Web.sitemap to Generate sitemap.xml</a></p>