128 lines
4.8 KiB
C#
128 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace XYLEM.Base.XML
|
|
{
|
|
public static class XMLStringClass
|
|
{
|
|
public enum XMLVersion
|
|
{
|
|
V1_0,
|
|
V1_1
|
|
}
|
|
|
|
/// <summary>
|
|
/// Strips non-printable ascii characters
|
|
/// Refer to http://www.w3.org/TR/xml11/#charsets for XML 1.1
|
|
/// </summary>
|
|
/// <param name="txtToClean"></param>
|
|
/// <returns></returns>
|
|
public static string StripIllegalXMLChars(this string txtToClean)
|
|
{
|
|
return StripIllegalXMLChars(txtToClean, XMLVersion.V1_1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Strips non-printable ascii characters
|
|
/// Refer to http://www.w3.org/TR/xml11/#charsets for XML 1.1
|
|
/// Refer to http://www.w3.org/TR/2006/REC-xml-20060816/#charsets for XML 1.0
|
|
/// </summary>
|
|
/// <param name="txtToClean"></param>
|
|
/// <param name="version"></param>
|
|
/// <returns></returns>
|
|
public static string StripIllegalXMLChars(this string txtToClean, XMLVersion version)
|
|
{
|
|
try
|
|
{
|
|
string pattern = String.Empty;
|
|
txtToClean = txtToClean.
|
|
Replace("&", "&").
|
|
Replace("<", "<").
|
|
Replace(">", ">").
|
|
Replace("\"", """).
|
|
Replace("'", "'").
|
|
Replace("\r", "
").
|
|
Replace("\n", "
");
|
|
switch (version)
|
|
{
|
|
case XMLVersion.V1_0:
|
|
pattern = @"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|7F|8[0-46-9A-F]9[0-9A-F])";
|
|
break;
|
|
case XMLVersion.V1_1:
|
|
pattern = @"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|[19][0-9A-F]|7F|8[0-46-9A-F]|0?[1-8BCEF])";
|
|
break;
|
|
}
|
|
|
|
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
|
|
if (regex.IsMatch(txtToClean))
|
|
{
|
|
txtToClean = regex.Replace(txtToClean, String.Empty);
|
|
}
|
|
if (txtToClean.Contains('\0'))
|
|
{
|
|
txtToClean = txtToClean.Remove(txtToClean.IndexOf('\0'));
|
|
}
|
|
if (txtToClean.Contains((char)0x1E))
|
|
{
|
|
txtToClean = txtToClean.Replace((char)0x1E, ' ');
|
|
}
|
|
{
|
|
// Replace any character that has survived this done before
|
|
// Replace illegal character in XML documents with blank
|
|
// See here for reference http://www.w3.org/TR/xml/#charsets
|
|
txtToClean = Regex.Replace(txtToClean, "[\x00-\x08\x0B\x0C\x0E-\x1F]", String.Empty, RegexOptions.Compiled);
|
|
}
|
|
return txtToClean;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error in XMLFileIOClass and StripIllegalXMLChars() " + (txtToClean != null ? " txtToClean = " + txtToClean.ToString() : "txtToClean is null"), ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// reinset non-printable ascii characters
|
|
/// Refer to http://www.w3.org/TR/xml11/#charsets for XML 1.1
|
|
/// </summary>
|
|
/// <param name="txtToClean"></param>
|
|
/// <returns></returns>
|
|
public static string ReinsetIllegalXMLChars(this string txtToClean)
|
|
{
|
|
return ReinsetIllegalXMLChars(txtToClean, XMLVersion.V1_1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// reinset non-printable ascii characters
|
|
/// Refer to http://www.w3.org/TR/xml11/#charsets for XML 1.1
|
|
/// Refer to http://www.w3.org/TR/2006/REC-xml-20060816/#charsets for XML 1.0
|
|
/// </summary>
|
|
/// <param name="txtToClean"></param>
|
|
/// <param name="version"></param>
|
|
/// <returns></returns>
|
|
public static string ReinsetIllegalXMLChars(string txtToReinset, XMLVersion version)
|
|
{
|
|
try
|
|
{
|
|
string pattern = String.Empty;
|
|
txtToReinset = txtToReinset.
|
|
Replace("&", "&").
|
|
Replace("<", "<").
|
|
Replace(">", ">").
|
|
Replace(""", "\"").
|
|
Replace("'", "'").
|
|
Replace("
", "\r").
|
|
Replace("
", "\n");
|
|
return txtToReinset;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error in XMLFileIOClass and StripIllegalXMLChars() " + (txtToReinset != null ? " txtToClean = " + txtToReinset.ToString() : "txtToClean is null"), ex);
|
|
}
|
|
}
|
|
}
|
|
}
|