13

I've a class correctly defined and after serialize it to XML I'm getting no encoding.

How can I define encoding "ISO-8859-1"?

Here's a sample code

var xml = new XmlSerializer(typeof(Transacao));
var file = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "transacao.xml"),FileMode.OpenOrCreate);            
xml.Serialize(file, transacao);            
file.Close();

Here are the beginning of xml generated

<?xml version="1.0"?>
<requisicao-transacao xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <dados-ec>
    <numero>1048664497</numero>
1

2 Answers 2

26

The following should work:

var xml = new XmlSerializer(typeof(Transacao));

var fname = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "transacao.xml");
var appendMode = false;
var encoding = Encoding.GetEncoding("ISO-8859-1");

using(StreamWriter sw = new StreamWriter(fname, appendMode, encoding))
{
    xml.Serialize(sw, transacao);
}

If you don't mind me asking, why do you need ISO-8859-1 encoding in particular? You could probably use UTF-8 or UTF-16 (they're more commonly recognizable) and get away with it.

Sign up to request clarification or add additional context in comments.

Comments

1

Create a StreamWriter with the desired encoding:

System.Text.Encoding code = *WhateverYouWant*
StreamWriter sw = new StreamWriter(file, code);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.