C# LoadXml 时发生 hexadecimal value 0x08, is an invalid character 错误

解决 XmlDocument 在调用 LoadXml 方法时发生 hexadecimal value 0x08, is an invalid character 和 hexadecimal value 0x12, is an invalid character 等错误。

原因是有很多符号不能在XML代码中出现,所以我们要替换掉:

private string ReplaceHexadecimalSymbols(string txt)
{
	if (txt != "")
	{
		string r = "[\x00-\x08\x0B\x0C\x0E-\x1F]";
		return Regex.Replace(txt, r, "", RegexOptions.Compiled);
	}
	else
	{
		return "";
	}
}