Wizard
Wizard

Reputation: 11295

c# xml special character encoding

I want load xml documents, but there are special symbols like : ąčęėįšųū and i get error Invalid character in the given encoding. Question is how to encode this characters before load xml ?

// load xml result from Google weather
XDocument xd = XDocument.Load("http://www.google.com/ig/api?weather=vilnius&hl=ru");

Upvotes: 0

Views: 1073

Answers (2)

Rob
Rob

Reputation: 1081

using (StreamReader sr = new StreamReader("http://www.google.com/ig/api?weather=vilnius&hl=ru", true))
{
    XDocument xdoc = XDocument.Load(sr);
}

The problem is with the encoding. If you use a StreamReader it should detect what encoding the response is in and then allow you to call XDocument.Load.

Upvotes: 1

L.B
L.B

Reputation: 116178

I would give this a try

WebClient cln = new WebClient();
var str = cln.DownloadString("http://www.google.com/ig/api?weather=vilnius&hl=ru");
XDocument xDoc = XDocument.Load(new StringReader(str));

Upvotes: 3

Related Questions