简体   繁体   中英

Custom encoding with XML serialization

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?

Here's a solution I've worked out. I created class that inherits from XmlTextWriter and overrode the WriteString method to run the string thru the Encoding class I created before writing it to the stream. This gives me the decimal encoding I need. Unfortunately, the ampersand from the decimal encoding is encoded (&#38 becomes &) so I have to use Replace to change it back. Other than that, it does the encoding in the manner I want. I've presented it to my team and the architects so we'll see what they say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM