简体   繁体   中英

XML Schema for Sitemap binding it together

I have the following code for defining XML Schema. Having a problem in keeping the lines together. Worked fine before.

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");
} 

SO this is now causing the following error:

在此处输入图片说明

Where am I doing it wrong? Thanks

Problem is in that "xsi:schemaLocation" bit I think - you can't have multiple lines between quotes in an XML attribute, if my brain remembers correctly. Try changing to:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM