diff --git a/src/Altinn.App.Core/Helpers/ObjectUtils.cs b/src/Altinn.App.Core/Helpers/ObjectUtils.cs
index e7f0f5e40..de84b0577 100644
--- a/src/Altinn.App.Core/Helpers/ObjectUtils.cs
+++ b/src/Altinn.App.Core/Helpers/ObjectUtils.cs
@@ -1,5 +1,6 @@
using System.Collections;
using System.Reflection;
+using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace Altinn.App.Core.Helpers;
@@ -7,7 +8,7 @@ namespace Altinn.App.Core.Helpers;
///
/// Utilities for working with model instances
///
-public static class ObjectUtils
+public static partial class ObjectUtils
{
///
/// Set empty Guid properties named "AltinnRowId" to a new random guid
@@ -137,14 +138,25 @@ public static void PrepareModelForXmlStorage(object model, int depth = 64)
SetToDefaultIfShouldSerializeFalse(model, prop, methodInfos);
// Set string properties with [XmlText] attribute to null if they are empty or whitespace
- if (
- value is string s
- && string.IsNullOrWhiteSpace(s)
- && prop.GetCustomAttribute() is not null
- )
+ if (value is string s)
{
- // Ensure empty strings are set to null
- prop.SetValue(model, null);
+ if (string.IsNullOrWhiteSpace(s) && prop.GetCustomAttribute() is not null)
+ {
+ // Ensure empty strings are set to null
+ prop.SetValue(model, null);
+ }
+ else
+ {
+ if (prop.SetMethod is not null)
+ {
+ // If a property doesn't have a setter, it hopefully doesn't have user input,
+ // and therefore it far less likely to have invalid XML chars. If that were the case
+ // we will still just error out when serializing to XML
+
+ // Remove invalid xml characters
+ prop.SetValue(model, XmlInvalidCharsRegex().Replace(s, "\uFFFD")); // \uFFFD is the unicode replacement character �
+ }
+ }
}
// continue recursion over all properties that are NOT null or value types
@@ -155,6 +167,13 @@ value is string s
}
}
+ // Regex copied from: https://stackoverflow.com/a/961504
+ // Which is based on spec: https://www.w3.org/TR/xml/#charsets
+ [GeneratedRegex(
+ @"(?