2016-07-22 89 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"); 

希望這有助於後來有人。