diff --git a/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs b/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs index 72ef05af..0d4d4df1 100644 --- a/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs +++ b/ProjectLighthouse/Helpers/Extensions/StringExtensions.cs @@ -6,5 +6,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers.Extensions public static class StringExtensions { public static string ToFileName(this string text) => Path.GetInvalidFileNameChars().Aggregate(text, (current, c) => current.Replace(c.ToString(), "")); + + public static string ToSafeXml(this string text) => text.Replace("<", "<").Replace(">", ">"); } } \ No newline at end of file diff --git a/ProjectLighthouse/Serialization/LbpSerializer.cs b/ProjectLighthouse/Serialization/LbpSerializer.cs index cfa64535..73a0c7f1 100644 --- a/ProjectLighthouse/Serialization/LbpSerializer.cs +++ b/ProjectLighthouse/Serialization/LbpSerializer.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; namespace LBPUnion.ProjectLighthouse.Serialization { @@ -12,22 +13,27 @@ namespace LBPUnion.ProjectLighthouse.Serialization [SuppressMessage("ReSharper", "UnusedMember.Global")] public static class LbpSerializer { + // IMPORTANT: All functions using values must call .ToSafeXml(); public static string BlankElement(string key) => $"<{key}>"; - public static string StringElement(KeyValuePair pair) => $"<{pair.Key}>{pair.Value}"; + public static string StringElement(KeyValuePair pair) => $"<{pair.Key}>{pair.Value.ToString().ToSafeXml()}"; - public static string StringElement(string key, bool value) => $"<{key}>{value.ToString().ToLower()}"; + public static string StringElement(string key, bool value) => $"<{key}>{value.ToString().ToLower().ToSafeXml()}"; - public static string StringElement(string key, object value) => $"<{key}>{value}"; + public static string StringElement(string key, object value) => $"<{key}>{value.ToString().ToSafeXml()}"; public static string TaggedStringElement (KeyValuePair pair, KeyValuePair tagPair) - => $"<{pair.Key} {tagPair.Key}=\"{tagPair.Value}\">{pair.Value}"; + => $"<{pair.Key} {tagPair.Key}=\"{tagPair.Value.ToString().ToSafeXml()}\">{pair.Value.ToString().ToSafeXml()}"; - public static string TaggedStringElement(string key, object value, string tagKey, object tagValue) => $"<{key} {tagKey}=\"{tagValue}\">{value}"; + public static string TaggedStringElement + (string key, object value, string tagKey, object tagValue) + => $"<{key} {tagKey}=\"{tagValue.ToString().ToSafeXml()}\">{value.ToString().ToSafeXml()}"; public static string TaggedStringElement(string key, object value, Dictionary attrKeyValuePairs) - => $"<{key} " + attrKeyValuePairs.Aggregate(string.Empty, (current, kvp) => current + $"{kvp.Key}=\"{kvp.Value}\" ") + $">{value}"; + => $"<{key} " + + attrKeyValuePairs.Aggregate(string.Empty, (current, kvp) => current + $"{kvp.Key}=\"{kvp.Value.ToString().ToSafeXml()}\" ") + + $">{value.ToString().ToSafeXml()}"; public static string Elements (params KeyValuePair[] pairs)