<PackageReference Include="NUnit" Version="4.2.2" />

XmlExtensions

static class XmlExtensions
Contains extension methods that do not require a special using directive.
using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text; using System.Xml; namespace NUnit.Framework { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal static class XmlExtensions { internal static readonly XmlWriterSettings FragmentWriterSettings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment }; internal static void WriteAttributeStringSafe(this XmlWriter writer, string name, string value) { writer.WriteAttributeString(name, EscapeInvalidXmlCharacters(value)); } internal static void WriteCDataSafe(this XmlWriter writer, string text) { if (text == null) throw new ArgumentNullException("text"); text = EscapeInvalidXmlCharacters(text); int num = 0; while (true) { int num2 = text.IndexOf("]]>", num, StringComparison.Ordinal); if (num2 < 0) break; writer.WriteCData(text.Substring(num, num2 - num + 2)); num = num2 + 2; if (num >= text.Length) return; } if (num > 0) writer.WriteCData(text.Substring(num)); else writer.WriteCData(text); } [System.Runtime.CompilerServices.NullableContext(2)] [return: NotNullIfNotNull("str")] internal static string EscapeInvalidXmlCharacters(string str) { if (str == null) return null; foreach (char c in str) { if (c < ' ' || c > '') return EscapeInvalidXmlCharactersUnlikely(str); } return str; } private static string EscapeInvalidXmlCharactersUnlikely(string str) { StringBuilder stringBuilder = null; for (int i = 0; i < str.Length; i++) { char c = str[i]; if (c > ' ' && c < '') stringBuilder?.Append(c); else if (('' > c || c > '\b') && c != ' ' && c != ' ' && ('' > c || c > '') && ('' > c || c > '„') && ('†' > c || c > 'Ÿ') && ('�' > c || c > '�') && c != '￾' && c != '￿') { stringBuilder?.Append(c); } else if (char.IsHighSurrogate(c) && i + 1 != str.Length && char.IsLowSurrogate(str[i + 1])) { if (stringBuilder != null) { stringBuilder.Append(c); stringBuilder.Append(str[i + 1]); } i++; } else { if (stringBuilder == null) { stringBuilder = new StringBuilder(); for (int j = 0; j < i; j++) { stringBuilder.Append(str[j]); } } stringBuilder.Append(CharToUnicodeSequence(c)); } } if (stringBuilder != null) return stringBuilder.ToString(); return str; } private static string CharToUnicodeSequence(char symbol) { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 1); defaultInterpolatedStringHandler.AppendLiteral("\\u"); defaultInterpolatedStringHandler.AppendFormatted((int)symbol, "x4"); return defaultInterpolatedStringHandler.ToStringAndClear(); } } }