XML hexadecimal value 0x__, is an invalid character
XML操作时异常:(十六进制值 0x__) 是无效的字符。
方法一:
设置 CheckCharacters=false。
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings { CheckCharacters = false };
方法二:
如果在第三方类库中,无法设置CheckCharacters ,那么只能移除无效的字符。 google下都是使用正则来移除,其实应该使用高效的位图算法。
类的静态方法:
static bool[] XMLInvalidCharMap = new bool[128];
//[\x00-\x08\x0B\x0C\x0E-\x1F\x26] for (int i = 0x00; i <= 0x08; i++) { XMLInvalidCharMap[i] = true; } XMLInvalidCharMap[0x0b] = true; XMLInvalidCharMap[0x0c] = true; for (int i = 0x0e; i <= 0x1f; i++) { XMLInvalidCharMap[i] = true; } //XMLInvalidCharMap[0x26] = true; //&符号,不应该被过滤
使用方法:
static string ReplaceHexadecimalSymbols(string txt) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < txt.Length; i++) { if (txt[i] < 128 && XMLInvalidCharMap[txt[i]]) { continue; } sb.Append(txt[i]); } return sb.ToString(); /* string r = "[\x00-\x08\x0B\x0C\x0E-\x1F\x26]"; return Regex.Replace(txt, r, "", RegexOptions.Compiled);*/ }
http://stackoverflow.com/questions/21053138/c-sharp-hexadecimal-value-0x12-is-an-invalid-character
https://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/