Domanda

public class HomeController : Controller
{
    public ActionResult SitemapXML()
    {
        return new XmlSiteMapResult();
    }
}

How generate sitemap in encoding utf-8 without BOM?

È stato utile?

Soluzione

Create a class to override the default behavior of XmlSiteMapResult so it excludes the BOM.

public class MyXmlSiteMapResult : XmlSiteMapResult
{

    /// <summary>
    /// Maximal number of links per sitemap file.
    /// </summary>
    /// <remarks>
    /// This number should be 50000 in theory, see http://www.sitemaps.org/protocol.php#sitemapIndex_sitemap.
    /// Since sitemap files can be maximal 10MB per file and calculating the total sitemap size would degrade performance,
    /// an average cap of 35000 has been chosen.
    /// </remarks>
    private const int MaxNumberOfLinksPerFile = 35000;

    protected override void ExecuteSitemapIndexResult(ControllerContext context, IEnumerable<SiteMapNode> flattenedHierarchy, long flattenedHierarchyCount)
    {
        // Count the number of pages
        double numPages = Math.Ceiling((double)flattenedHierarchyCount / MaxNumberOfLinksPerFile);

        // Output content type
        context.HttpContext.Response.ContentType = "text/xml";

        // Generate sitemap sitemapindex
        var sitemapIndex = new XElement(Ns + "sitemapindex");
        sitemapIndex.Add(GenerateSiteMapIndexElements(Convert.ToInt32(numPages), Url, SiteMapUrlTemplate).ToArray());

        // Generate sitemap
        var xmlSiteMap = new XDocument(
            new XDeclaration("1.0", "utf-8", "true"),
            sitemapIndex);

        // Specify to emit XML with no BOM
        var settings = new XmlWriterSettings();
        settings.Encoding = new System.Text.UTF8Encoding(false);

        // Write XML
        using (Stream outputStream = RetrieveOutputStream(context))
        {
            using (var writer = XmlWriter.Create(outputStream, settings))
            {
                xmlSiteMap.WriteTo(writer);
            }
            outputStream.Flush();
        }
    }

    protected override void ExecuteSitemapResult(ControllerContext context, IEnumerable<SiteMapNode> flattenedHierarchy, long flattenedHierarchyCount, int page)
    {

        // Output content type
        context.HttpContext.Response.ContentType = "text/xml";

        // Generate URL set
        var urlSet = new XElement(Ns + "urlset");
        urlSet.Add(GenerateUrlElements(
            flattenedHierarchy.Skip((page - 1)* MaxNumberOfLinksPerFile)
                .Take(MaxNumberOfLinksPerFile), Url).ToArray());

        // Generate sitemap
        var xmlSiteMap = new XDocument(
            new XDeclaration("1.0", "utf-8", "true"),
            urlSet);

        // Specify to emit XML with no BOM
        var settings = new XmlWriterSettings();
        settings.Encoding = new System.Text.UTF8Encoding(false);

        // Write XML
        using (Stream outputStream = RetrieveOutputStream(context))
        {
            using (var writer = XmlWriter.Create(outputStream, settings))
            {
                xmlSiteMap.WriteTo(writer);
            }
            outputStream.Flush();
        }
    }
}

And then return your custom class instead.

public class HomeController : Controller
{
    public ActionResult SitemapXML()
    {
        return new MyXmlSiteMapResult();
    }
}

NOTE: This example assumes you are using v3 of MvcSiteMapProvider, since in v4, the XmlSiteMapResult class no longer has a default constructor (in v4, you must use the XmlSiteMapResultFactory to get an instance of XmlSiteMapResult so you will also need to override XmlSiteMapResultFactory to return an instance of your custom class).

Reference: How can I remove the BOM from XmlTextWriter using C#?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top