2013-04-08 35 views
0

我有以下用於定義XML架構的代碼。在保持線條一致方面存在問題。之前工作良好。XML架構將Sitemap綁定在一起

public static FileContentResult WriteTo(SiteMapFeed feedToFormat) 
{ 
    var siteMap = feedToFormat;   

    //TODO: DO something, next codes are just DEMO 
    var header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
     + Environment.NewLine + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"" 
     + Environment.NewLine + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 
     + Environment.NewLine + "xsi:schemaLocation=\"" 
     + Environment.NewLine + "http://www.sitemaps.org/schemas/sitemap/0.9" 
     + Environment.NewLine + "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">"; 

    var urls = new System.Text.StringBuilder();   
    foreach (var site in siteMap.Items) 
    { 
     urls.Append(string.Format("<url><loc>http://www.{0}/</loc><lastmod>{1}</lastmod><changefreq>{2}</changefreq><priority>{3}</priority></url>", site.Url, site.LastMod, site.ChangeFreq, site.Priority)); 

    }      
    byte[] fileContent = System.Text.Encoding.UTF8.GetBytes(header + urls + "</urlset>"); 
    return new FileContentResult(fileContent, "text/xml"); 
} 

所以這是現在導致以下錯誤:

enter image description here

我在哪裏做錯了?謝謝

回答

0

問題在於「xsi:schemaLocation」位我認爲 - 如果我的大腦記得正確,那麼XML屬性中的引號之間不能有多行。嘗試改爲:

var header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
    + Environment.NewLine + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"" 
    + Environment.NewLine + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 
    + Environment.NewLine + "xsi:schemaLocation=\"" 
    + "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">" + Environment.NewLine;