Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

TranslationService.cs - add xsd schema for importing xml

supporte960c76f49c0
diff --git a/src/Business/Grand.Business.Common/Services/Localization/TranslationService.cs b/src/Business/Grand.Business.Common/Services/Localization/TranslationService.cs
index f0f81127f..65275227d 100644
--- a/src/Business/Grand.Business.Common/Services/Localization/TranslationService.cs
+++ b/src/Business/Grand.Business.Common/Services/Localization/TranslationService.cs
@@ -1,3 +1,4 @@
+using Grand.Business.Common.Utilities;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Data;
using Grand.Domain.Localization;
@@ -7,6 +8,7 @@ using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Extensions;
using MediatR;
using System.Xml;
+using System.Xml.Schema;
namespace Grand.Business.Common.Services.Localization
{
@@ -249,9 +251,8 @@ namespace Grand.Business.Common.Services.Localization
return;
var translateResources = new List<TranslationResource>();
- //stored procedures aren't supported
- var xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(xml);
+
+ var xmlDoc = LanguageXmlDocument(xml);
var nodes = xmlDoc.SelectNodes(@"//Language/Resource");
if (nodes != null)
@@ -312,9 +313,8 @@ namespace Grand.Business.Common.Services.Localization
if (string.IsNullOrEmpty(xml))
return;
-
- var xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(xml);
+
+ var xmlDoc = LanguageXmlDocument(xml);
var translateResources = new List<TranslationResource>();
@@ -348,6 +348,18 @@ namespace Grand.Business.Common.Services.Localization
//clear cache
await _cacheBase.RemoveByPrefix(CacheKey.TRANSLATERESOURCES_PATTERN_KEY);
}
+ private static XmlDocument LanguageXmlDocument(string xml)
+ {
+ var schemas = new XmlSchemaSet();
+ schemas.Add("", XmlReader.Create(new StringReader(LanguageSchema.SchemaXsd)));
+
+ var xmlDoc = new XmlDocument { Schemas = schemas, XmlResolver = null };
+ xmlDoc.LoadXml(xml);
+
+ // Validate XML.
+ xmlDoc.Validate((_, e) => throw new XmlException("XML data does not conform to the schema", e.Exception));
+ return xmlDoc;
+ }
#endregion
}
diff --git a/src/Business/Grand.Business.Common/Utilities/LanguageSchema.cs b/src/Business/Grand.Business.Common/Utilities/LanguageSchema.cs
new file mode 100644
index 000000000..a5a063b18
--- /dev/null
+++ b/src/Business/Grand.Business.Common/Utilities/LanguageSchema.cs
@@ -0,0 +1,24 @@
+namespace Grand.Business.Common.Utilities;
+
+public static class LanguageSchema
+{
+ public const string SchemaXsd = @"
+ <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
+ <xs:element name='Language'>
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name='Resource' maxOccurs='unbounded'>
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name='Value' type='xs:string' />
+ </xs:sequence>
+ <xs:attribute name='Name' type='xs:string' use='required' />
+ <xs:attribute name='Area' type='xs:string' use='optional' />
+ </xs:complexType>
+ </xs:element>
+ </xs:sequence>
+ <xs:attribute name='Name' type='xs:string' use='required' />
+ </xs:complexType>
+ </xs:element>
+ </xs:schema>";
+}
\ No newline at end of file