我在Visual Studio中有一个C#项目,它下载并解析包含韩文,中文和其他unicode字符的XML文件。例如,对于名为 Taeyang 的韩国艺术家,它会生成如下XML:
<name>태양</name>
但它返回
<name>??</name>
我尝试了StreamReader Encoding.Default
,但结果是
<name>태양</name>
代码:
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();
和xml源:
<?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>
我很困惑,为什么会这样?有什么想法吗?
答案 0 :(得分:4)
使用下面的代码(通知我注释掉你的2行)
//newRequest.Headers["If-None-Match"] = "d84e5667-3cbe-4556-b551-9d7e4be95d71";
//newRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
并更改了您的行:StreamReader(stream, Encoding.UTF-8);
至:StreamReader(stream, Encoding.UTF8);
我明智地得到了一个好结果:
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);
答案 1 :(得分:0)
尝试UTF8编码
StreamReader sr= new StreamReader(file_name, System.Text.Encoding.UTF8);
答案 2 :(得分:0)
我发现Console.WriteLine()
无法清楚地输出unicode。所有unicode(例如韩语,中文)和除a-z和0-9之外的所有字符都不能按预期输出,因为Console.WriteLine()
使用单字体Raster Font
但主要问题是关于我的数据库连接,我忘记在我的连接字符串中添加charset=utf-8