Mike
Mike

Reputation: 1231

C# StreamReader Encoding.UTF8 not working

I have a C# Project in Visual studio which download and parse XML file that contains Korean, Chinese and another unicode characters. For example for korean artist named Taeyang it produce XML like this :

<name>태양</name>

but it returns

<name>??</name>

I have tried StreamReader Encoding.Default but result is

<name>태양</name>

The code:

string address = String.Format("http://musicbrainz.org/ws/2/artist/{0}?inc=url-rels", mbids[ord]);
HttpWebRequest newRequest = WebRequest.Create(address) as HttpWebRequest;
               newRequest.Headers["If-None-Match"] = etagProf;
               newRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
var response = newRequest.GetResponse();
// Reader
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF-8);
string data = reader.ReadToEnd();

and the xml source:

<?xml version="1.0" encoding="UTF-8"?>
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">
    <artist type="Person" id="d84e5667-3cbe-4556-b551-9d7e4be95d71">   
        <name>태양</name>
        <sort-name>Taeyang</sort-name><gender>Male</gender>
        <country>KR</country>
        ...........
    </artist>
</metadata>

I'm confused, why it happens ? Any idea dude ?

Upvotes: 4

Views: 9693

Answers (3)

Mike
Mike

Reputation: 1231

I found that Console.WriteLine() can't output unicode clearly. Everything unicode (e.g. Korean, Chinese) and all characters except a-z and 0-9 can't output as expected cause Console.WriteLine() use single font Raster Font

But the main problem was about my DB CONNECTION, i forget to add charset=utf-8 in my connection string

Upvotes: 0

Sagiv b.g
Sagiv b.g

Reputation: 31024

using the code below (notice I comment out 2 of your lines)

//newRequest.Headers["If-None-Match"] = "d84e5667-3cbe-4556-b551-9d7e4be95d71";
//newRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";

and changed your line: StreamReader(stream, Encoding.UTF-8);

to : StreamReader(stream, Encoding.UTF8);

I got a good result characters wise: enter image description here

string address = String.Format("http://musicbrainz.org/ws/2/artist/{0}?inc=url-rels","d84e5667-3cbe-4556-b551-9d7e4be95d71");
HttpWebRequest newRequest = WebRequest.Create(address) as HttpWebRequest;
//newRequest.Headers["If-None-Match"] = "d84e5667-3cbe-4556-b551-9d7e4be95d71";
//newRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
var response = newRequest.GetResponse();
// Reader
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string data = reader.ReadToEnd();
MessageBox.Show(data);

Upvotes: 6

chouaib
chouaib

Reputation: 2817

try UTF8 Encoding

StreamReader sr= new StreamReader(file_name, System.Text.Encoding.UTF8);

Upvotes: 0

Related Questions