Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamReader from MemoryStream UTF8 Encoding

I want to open a XML file (from an zip archive) in a MemoryStream and create a StreamReader form this stream to put it into a GridView.

I use this code :

MemoryStream ms = new MemoryStream();
entry.Extract(ms);
StreamReader reader = new StreamReader(ms);
DataSet ds = new DataSet();
ds.ReadXml(reader);
dataGridView1.DataSource = GlobalDs.Tables[0];

If my XML files are encoded in ANSI, it works perfectly. But when I load files encoded in UTF8, it fail, even I initialize the StreamReader like that :

StreamReader reader = new StreamReader(ms, System.Text.Encoding.UTF8);

I hope someone will have an idea to solve my problem.

like image 947
flow Avatar asked Feb 13 '13 14:02

flow


1 Answers

Try using:

StreamReader reader = new StreamReader(ms, System.Text.Encoding.UTF8, true);

The third param is for detectEncodingFromByteOrderMarks (msdn)

like image 160
Yaakov Ellis Avatar answered Oct 20 '22 00:10

Yaakov Ellis