简体   繁体   English

如何使用WebClient DownloadData获取unicode字符串?

[英]How to get unicode string with WebClient DownloadData?

Sorry for my bad English. 对不起,我的英语不好。

I am trying to get a string data with this code: 我试图用这段代码获取字符串数据:

WebClient wc = new WebClient();
byte[] buffer = wc.DownloadData("http://......);
string xml = Encoding.UTF8.GetString(buffer);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

the string has Unicode data. 该字符串具有Unicode数据。 when I get this with my browser like firefox every things are ok. 当我用我的浏览器像firefox这样的东西都可以。

But in my code the string is broken and xml file is useless. 但在我的代码中,字符串被破坏,xml文件无用。 Some characters changed to their 有些人物变成了他们的

decimal value and when reading xml file they are only characters that we can read. 十进制值,当读取xml文件时,它们只是我们可以读取的字符。 and others 和别的

changed to strange signs. 变成了奇怪的迹象。

Do you know how can I do? 你知道我该怎么办?

Put your data into a stream: 将您的数据放入流中:

var stream = new MemoryStream(buffer);

And load it with the Load method: 并使用Load方法加载它:

doc.Load(stream);

This will try to detect the correct encoding. 这将尝试检测正确的编码。

Or maybe WebClient.DownloadString will work as well. 或者也许WebClient.DownloadString也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM