3

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 am getting following ERROR in browser for 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 have removed all spaces from View(Sitemap.cshtml). but still error

I even tried this and inverse the order as shown below but still 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 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.

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 get my head around why? Any Idea?

I checked following but still could not able to find answer

How to create XML sitemap programmatically in c#

sitemaps.xml in asp.net mvc

ASP.NET Web.sitemap to Generate sitemap.xml

Community
  • 1
  • 1
Developer
  • 25,073
  • 20
  • 81
  • 128

1 Answers1

10

I fixed the problem by adding XML tag in Response.Write

@{   
    Response.Write("<?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>

Thanks to EvanSwd

Developer
  • 25,073
  • 20
  • 81
  • 128
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. – icebat Nov 13 '14 at 14:21
  • @icebat Answers should be in answers section whether it is complete or partial. Question section should only hold additional information regarding questions not answers. – Developer Nov 13 '14 at 14:54