TheNoob
TheNoob

Reputation: 929

ASP.NET MVC Sitemap.xml error

I have a valid sitemap.xml file. The problem arises when I try to serve this file as a sitemap.xml. I get the following error:

This page contains the following errors:

error on line 1 at column 95: Extra content at the end of the document
Below is a rendering of the page up to the first error.

When I inspect /sitemap.xml from browser each element tag gets this added to it.

<url xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    the rest
</url>

Here is how I return the file from the controller:

XmlDocument xml = new XmlDocument();
xml.Load(@"C:\sitemap.xml");
return Content(xml.DocumentElement.InnerXml, "application/xml");

Here is an example of the file I have and trying to return

<?xml version="1.0" encoding="utf-8"?>
<urlset 
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  <url>
    <loc>LINK</loc>
  </url>
  THE REST OF URLS
</urlset>

I have tried switching the "application/xml" to "text/xml" but didn't solve this problem. Am I not using XmlDocument correctly or am I not fully understanding what happens with return Content()?

Any help is appreciated.

Thank you

Upvotes: 1

Views: 831

Answers (1)

TheNoob
TheNoob

Reputation: 929

What ended up fixed this was a simple fix.

XmlDocument xml = new XmlDocument();
xml.Load(@"C:\sitemap.xml");
return Content(xml.DocumentElement.InnerXml, "application/xml");

Changed to

XmlDocument xml = new XmlDocument();
xml.Load(@"C:\sitemap.xml");
return Content(xml.DocumentElement.OuterXml, "application/xml");

Hope this helps someone later.

Upvotes: 1

Related Questions