My Controller action looks like this.
public ActionResult Sitemap()
{
Response.ContentType = "application/xml";
return View();
}
My View Sitemap.cshtml looks like this.
@{Layout = null;}<?xml version='1.0' encoding='UTF-8' ?>
<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">
<url>
<loc>https://www.mywebsitename.com/home</loc>
<lastmod>2006-12-12</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
</urlset>
I get the following ERROR in the browser for the page ( http://localhost:50831/Sitemap)
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.
I removed all spaces from the View (Sitemap.cshtml). but still a mistake
I even tried this and the reverse order as shown below, but still an error.
<?xml version='1.0' encoding='UTF-8' ?>
@{ Layout = null;}
<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">
<url>
<loc>https://www.mywebsitename.com/home</loc>
<lastmod>2006-12-12</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
</urlset>
It looks like it sends an empty space or line before the header, which causes the error above. The layout is null, the view starts with the xml tag and from where it gets the empty space.
page source output
----EMPTY LINE----
<?xml version='1.0' encoding='UTF-8' ?>
<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">
<url>
<loc>https://www.mywebsitename.com/home</loc>
<lastmod>2006-12-12</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
</urlset>
I could not understand why? Any idea?
I checked the following, but still could not find the answer
How to create an XML sitemap programmatically in C #
sitemaps.xml in asp.net mvc
ASP.NET Web.sitemap to create sitemap.xml