Changing encoding on generated serialized XML

Tucalipe

Member
Joined
Jul 7, 2017
Messages
8
Programming Experience
Beginner
I need to create a serialized XML under an utf-8 encoding. I've successfully serialized the fields required by an external library, and I can successfully generate an utf-16 xml. However, the device I need to send this xml to only accepts utf-8 encoding.

C#:
            StringWriter writer = new StringWriter();            XmlSerializer serializer = new XmlSerializer(CFe.GetType());
            XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
            xns.Add(string.Empty, string.Empty);
            serializer.Serialize(writer, CFe, xns);
            string XmlFinal = writer.ToString();
            File.WriteAllText(@"C:\venda.xml", XmlFinal, Encoding.UTF8); //This is just to generate a xml file for my evaluation purposes, generating a xml file is not required at this moment.
            //File.WriteAllText(@"C:\venda.txt", String.Join(" | ", SATFuncoes.SendSaleData("12345678", XmlFinal))) //This prints the device's response after trying to send the xml to be processed.

Even using "Encoding.UTF8 on FileWriteAllText, when opening the file on Notepad++ it gives me a "Document labelled UTF-16 but has UTF-8 content." error.
If I try to send the xml directly to my device, it prints a "No support to change the encoding from the currently used to the specified encoding.", meaning the device can't convert from UTF-16 to UTF-8 itself, it only accepts UTF-8 xml strings.

How can I force an UTF-8 encoding while sending data to my device?


EDIT: I've found a supposed solution that had me create a class:
C#:
        public class Utf8StringWriter : StringWriter        {


            public override Encoding Encoding
            {
                get { return new UTF8Encoding(false); }
            }
        }

And use
C#:
            StringWriter writer = new Utf8StringWriter();
as my StringWriter. This one DOES output an utf-8 encoded xml, but the tabs (  ) are replaced with double spaces ( ), which apparently my device does not understand...

More precisely, my device expects a single line utf-8 string like this:
C#:
<?xml version="1.0" encoding="utf-8"?><CFe><InfCFe versaoDadosEnt="0.07"><ide><CNPJ>16716114000172</CNPJ><signAC>SGR-SAT SISTEMA DE GESTAO E RETAGUARDA DO SAT</signAC></infCFe></CFe>

EDIT:

I've managed to do so using:
C#:
            StringWriter writer = new Utf8StringWriter();
            XmlWriterSettings settings = new XmlWriterSettings() { Encoding = new System.Text.UTF8Encoding(false)};
            XmlWriter xwriter = XmlWriter.Create(@"C:\Venda2.xml", settings);
            StringBuilder sb = new StringBuilder();
            XmlWriter xwriter2 = XmlWriter.Create(sb, settings);
            XmlSerializer serializer = new XmlSerializer(CFe.GetType());
            XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
            xns.Add(string.Empty, string.Empty);
            serializer.Serialize(xwriter, CFe, xns);
            serializer.Serialize(xwriter2, CFe, xns);
            string XmlFinal = writer.ToString();

With worthy mention of Encoding = new System.Text.UTF8Encoding(false), which will prevent the encoding of a BOM.
 
Last edited:

Latest posts

Back
Top Bottom