Farzin Kanzi
Farzin Kanzi

Reputation: 3435

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. when I get this with my browser like firefox every things are ok.

But in my code the string is broken and xml file is useless. Some characters changed to their

decimal value and when reading xml file they are only characters that we can read. and others

changed to strange signs.

Do you know how can I do?

Upvotes: 1

Views: 1203

Answers (1)

fejesjoco
fejesjoco

Reputation: 11903

Put your data into a stream:

var stream = new MemoryStream(buffer);

And load it with the Load method:

doc.Load(stream);

This will try to detect the correct encoding.

Or maybe WebClient.DownloadString will work as well.

Upvotes: 3

Related Questions