I am working on a project where we are serializing a class into an XML string. The requirements state that the encoding should be UTF-8, but they want characters with an ASCII values > 127 to be decimal encoded. Plus, they don't want entity encoding (&, <, and so on); they want those characters to be decimal encoded too.
Currently, we are doing the standard UTF-8 encoding and "pre-encoding" the special characters when we place them in the object. That means that any ampersands from that encoding are going to be encoded when we serialize so we have an extra step to undo that encoding.
I've found a way to create an encoding class that inherits from Encoding and overrides the GetBytes method. It worked great when I ran it standalone, but when I used it in the XmlWriterSettings, it doesn't call the method that I overrode. Instead, I get a 501 error with the exception message "No data is available for encoding 0. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method." The doc for Encoding.RegisterProvider says that is it available since .Net 4.6, but I'm using 4.5.2.
Is there a way override the encoding so I can manually encode the attribute and element values?