0

I'm getting the following error

Hexadecimal value 0x02, is an invalid character

when I'm generating the XML document

using MemoryStream ms = new();
var XML = new XmlSerializer(typeof(InvoiceType));
var encoding = Encoding.UTF8;

using (StreamWriter sw = new StreamWriter(ms, encoding))
{
    XML.Serialize(sw, doc, namespace);
}

return ms.ToArray();

The exception is thrown from the Serialize() method; I tried to encode it but it didn't solve the issue, I also tried to encode the string that caused the error before passing it to the document but with no luck

Can you please help me with this one?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vega_gf
  • 170
  • 1
  • 10
  • The error is about invalid characters containing in the document you are serializing. Can you please provide value of your variable `doc`? – SNBS Nov 26 '22 at 17:54
  • 1
    It must be that somewhere in your `InvoiceType doc` object, you have a string which includes a [U+0002](https://www.compart.com/en/unicode/U+0002) character. This character is not allowed by the XML standard. The valid character ranges can be found in the XML standard here: https://www.w3.org/TR/REC-xml/#NT-Char. – dbc Nov 26 '22 at 18:14
  • Are you actually trying to store binary data inside a `string` perhaps? Or is the `0x02` character there due to some sort of bug? Can you share a [mcve] showing how the `doc` containing this string was created? – dbc Nov 26 '22 at 18:15
  • If you are storing binary data in a string, it would be better to store it in a `byte []` array. `XmlSerializer` will automatically serialize byte arrays as Base64. If the character is creeping in erroneously but you must serialize it anyway (even though your XML will be malformed if you do) you could set `XmlWriterSettings.CheckCharacters = false` as shown in [this answer](https://stackoverflow.com/a/32888973/3744182) by [lucas teles](https://stackoverflow.com/users/3447313/lucas-teles) to [Dealing with invalid XML hexadecimal characters](https://stackoverflow.com/q/8170739/3744182). – dbc Nov 26 '22 at 18:19
  • You need to find out exactly where in your c# classes the exception is occurring. Xml serialize methods are very hard to debug. The best way I found to debug the problems is to comment out the classes/property until I find the root cause of the issue. Normal Xml Serialize should not generate the error you are getting. I suspect there is some custom serialization occurring. Binary data should be automatically encoded. – jdweng Nov 26 '22 at 21:29

1 Answers1

0

I resolved this issue by sanitizing the source of error (customer input) using this method

public static string RemoveInvalidXmlChars(string content)
{
     return  new string(content.Where(ch =>System.Xml.XmlConvert.IsXmlChar(ch)).ToArray());
}

credit to @Alex Vazhev

link

vega_gf
  • 170
  • 1
  • 10