From 06100ca6dd314ad78c825b169fe2882489e99db3 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Fri, 3 Jul 2026 05:30:23 +0330 Subject: [PATCH] fix: update sitemap generation to use XmlWriter for improved XML handling - Changed the encoding in the XML declaration from "UTF-8" to "utf-8" for consistency. - Replaced StringWriter with XmlWriter to enhance XML document writing and ensure proper encoding settings. - Updated the return statement to convert the MemoryStream to a UTF-8 string, improving the efficiency of the sitemap generation process. These changes enhance the reliability and correctness of the sitemap generation, ensuring better compliance with XML standards. --- .../Utilities/Seo/SitemapGenerator.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/FrontOffice.Main/Utilities/Seo/SitemapGenerator.cs b/src/FrontOffice.Main/Utilities/Seo/SitemapGenerator.cs index e65e3c0..1739f2b 100644 --- a/src/FrontOffice.Main/Utilities/Seo/SitemapGenerator.cs +++ b/src/FrontOffice.Main/Utilities/Seo/SitemapGenerator.cs @@ -1,4 +1,5 @@ using System.Text; +using System.Xml; using System.Xml.Linq; using Microsoft.Extensions.Options; @@ -71,16 +72,23 @@ public class SitemapGenerator } var document = new XDocument( - new XDeclaration("1.0", "UTF-8", null), + new XDeclaration("1.0", "utf-8", null), new XElement(ns + "urlset", urls)); - var builder = new StringBuilder(); - using (var writer = new StringWriter(builder)) + var settings = new XmlWriterSettings + { + Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), + Indent = true, + OmitXmlDeclaration = false + }; + + using var stream = new MemoryStream(); + using (var writer = XmlWriter.Create(stream, settings)) { document.Save(writer); } - return builder.ToString(); + return Encoding.UTF8.GetString(stream.ToArray()); } private static XElement CreateUrlElement(