十六进制值0x0B,是XML中的无效字符问题

时间:2015-03-27 12:57:02

标签: asp.net xml

enter image description here

我正在异常

  

'',十六进制值0x0B,是无效字符。第23行,第22位。

我已经尝试过来自Here的解决方案,但它对我不起作用。由于我的项目是 3.5 版本,我无法使用XmlConvert.IsXmlChar方法MSDN

如何处理?

1 个答案:

答案 0 :(得分:3)

您可以使用以下方法替换这些无效字符。

public static string CleanInvalidXmlChars(this string StrInput)
    {
        //Returns same value if the value is empty.
        if (string.IsNullOrWhiteSpace(StrInput))
        {
            return StrInput;
        }
        // From xml spec valid chars:
        // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]    
        // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
        string RegularExp = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]";
        return Regex.Replace(StrInput, RegularExp, String.Empty);
    }