10
votes

enter image description here

I am getting exception

'', hexadecimal value 0x0B, is an invalid character. Line 23, position 22.

I have already tried solution from Here, but it is not working for me. As my project is in 3.5 version, I cannot use XmlConvert.IsXmlChar method MSDN

How to handle it ?

1

1 Answers

7
votes

You can replace these invalid characters using the below method.

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);
    }