/XML.cs |
@@ -4,15 +4,28 @@ |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Linq.Expressions; |
using System.Text; |
using System.Text.RegularExpressions; |
using System.Threading.Tasks; |
using System.Xml.Linq; |
|
namespace wasSharp |
{ |
public class XML |
public static class XML |
{ |
private static readonly Func<string, bool> directIsSafeXML = |
((Expression<Func<string, bool>>) |
(data => |
Regex.Replace(data, |
@"(" + string.Join("|", @"&", @"<", @">", @""", @"'") + @")", |
@"", RegexOptions.IgnoreCase | RegexOptions.Multiline) |
.IndexOfAny(new[] {'&', '<', '>', '"', '\''}) |
.Equals(-1))).Compile(); |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
@@ -23,9 +36,9 @@ |
/// <returns>an XML unescaped string</returns> |
public static string UnescapeXML(string s) |
{ |
Queue<char> t = new Queue<char>(); |
StringBuilder m = new StringBuilder(); |
foreach (char c in s) |
var t = new Queue<char>(); |
var m = new StringBuilder(); |
foreach (var c in s) |
{ |
switch (c) |
{ |
@@ -41,7 +54,7 @@ |
if (!t.Count.Equals(0)) |
{ |
t.Enqueue(c); |
string special = string.Join("", t.ToArray()); |
var special = string.Join("", t.ToArray()); |
switch (special) |
{ |
case "'": |
@@ -98,7 +111,7 @@ |
{ |
if (string.IsNullOrEmpty(s)) return s; |
|
string[] result = new string[s.Length]; |
var result = new string[s.Length]; |
Parallel.ForEach(Enumerable.Range(0, s.Length), o => |
{ |
switch (s[o]) |
@@ -125,5 +138,40 @@ |
}); |
return string.Join("", result); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Determines whether a string is safe to use in XML |
/// </summary> |
/// <param name="data">the string to check</param> |
/// <returns>true in case the string is safe</returns> |
public static bool IsSafeXML(string data) |
{ |
return directIsSafeXML(data); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Recursively rename a node by name. |
/// </summary> |
/// <param name="root">the root from where to start</param> |
/// <param name="name">the name to replace</param> |
/// <param name="rename">the name to replace with</param> |
public static void RenameNodes(XElement root, string name, string rename) |
{ |
if (Strings.StringEquals(root.Name.LocalName, name, StringComparison.Ordinal)) |
{ |
root.Name = rename; |
} |
|
foreach (var xElement in root.Elements()) |
{ |
RenameNodes(xElement, name, rename); |
} |
} |
} |
} |