c#xml特殊字符编码

时间:2012-05-04 21:16:31

标签: c# asp.net character-encoding

我想加载xml文档,但是有一些特殊的符号,如:ąčęėįšųū,我得到错误Invalid character in the given encoding.问题是如何在加载xml之前对这些字符进行编码?

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

2 个答案:

答案 0 :(得分:3)

我会尝试一下

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));

答案 1 :(得分:1)

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

问题在于编码。如果您使用StreamReader,它应该检测响应的编码,然后允许您调用XDocument.Load。