NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
XmlUtils.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using System.Text;
10
11namespace NanoXLSX.Utils.Xml
12{
17 public static class XmlUtils
18 {
25 public static string SanitizeXmlValue(string input)
26 {
27 if (input == null) { return ""; }
28 var len = input.Length;
29 var illegalCharacters = new List<int>(len);
30 int i;
31 for (i = 0; i < len; i++)
32 {
33 if (char.IsSurrogate(input[i]))
34 {
35 if (i + 1 < input.Length && char.IsSurrogatePair(input[i], input[i + 1]))
36 {
37 // Valid surrogate pair; append both characters as-is.
38 i++; // Skip the next character.
39 continue;
40 }
41 else
42 {
43 illegalCharacters.Add(i);
44 continue;
45 }
46 }
47 if (input[i] < 0x9 || input[i] > 0xA && input[i] < 0xD || input[i] > 0xD && input[i] < 0x20 || input[i] > 0xD7FF && input[i] < 0xE000 || input[i] > 0xFFFD)
48 {
49 illegalCharacters.Add(i);
50 continue;
51 } // Note: XML specs allow characters up to 0x10FFFF. However, the C# char range is only up to 0xFFFF; Higher values are neglected here
52 }
53 if (illegalCharacters.Count == 0)
54 {
55 return input;
56 }
57
58 var sb = new StringBuilder(len);
59 var lastIndex = 0;
60 len = illegalCharacters.Count;
61 for (i = 0; i < len; i++)
62 {
63 sb.Append(input.Substring(lastIndex, illegalCharacters[i] - lastIndex));
64 sb.Append(' '); // Whitespace as fall back on illegal character
65 lastIndex = illegalCharacters[i] + 1;
66 }
67 sb.Append(input.Substring(lastIndex));
68 return sb.ToString();
69 }
70 }
71}
Class providing static methods to manipulate XML during packing or unpacking.
Definition XmlUtils.cs:18
static string SanitizeXmlValue(string input)
Method to sanitize XML string values between two XML tags or in an attribute. Not considered are '<',...
Definition XmlUtils.cs:25