11

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 ?

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
user1926138
  • 1,464
  • 7
  • 34
  • 53
  • possible duplicate of [XMLTextReader is created but XslCompiledTransform.Transform fails with invalid character](http://stackoverflow.com/questions/28282249/xmltextreader-is-created-but-xslcompiledtransform-transform-fails-with-invalid-c) – GSerg Mar 27 '15 at 12:59
  • 1
    Are you able to edit your [XML Character Range](http://www.w3.org/TR/xml/#charsets)? – Matheus Moretti Rangel Mar 27 '15 at 13:05

2 Answers2

8

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);
    }
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
0

If you don't want to remove the vertical tab (hexadecimal value 0x0B) from the string (e.g. database export), you can also set CheckCharacters to false in your XmlWriterSettings.

Gets or sets a value that indicates whether the XML writer should check to ensure that all characters in the document conform to the "2.2 Characters" section of the W3C XML 1.0 Recommendation. Returns: true to do character checking; otherwise, false. The default is true.

e.g.

private static System.Xml.XmlWriter CreateXmlWriter(System.IO.Stream stream)
{
    System.Xml.XmlWriterSettings xs = new System.Xml.XmlWriterSettings();
    xs.Indent = true;
    xs.IndentChars = "    ";
    xs.NewLineChars = System.Environment.NewLine;
    xs.OmitXmlDeclaration = false; // // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    // xs.Encoding = System.Text.Encoding.UTF8; // doesn't work with pgsql 
    // xs.Encoding = new System.Text.UTF8Encoding(false);
    xs.Encoding = new System.Text.UTF8Encoding(false, false);
    xs.Async = true;
    xs.CheckCharacters = false;
    
    return System.Xml.XmlWriter.Create(stream, xs);
} // End Function CreateXmlWriter 
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442