2016-07-22 90 views
1

我有一个有效的sitemap.xml文件。当我尝试将此文件作为sitemap.xml提供时,会出现问题。我收到以下错误:ASP.NET MVC Sitemap.xml错误

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. 

当我从浏览器检查/ sitemap.xml时,每个元素标记都会将其添加到它。

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

这里是我从控制器返回文件:

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

这里是我有一个文件的例子,试图返回

<?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> 

我曾尝试切换“application/xml”改为“text/xml”,但没有解决这个问题。我没有正确使用XmlDocument,或者我不完全理解返回Content()会发生什么?

任何帮助表示赞赏。

谢谢

回答

1

什么结束了修复这是一个简单的修复。

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

改为

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

希望这有助于后来有人。