我想加载 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");
    我想加载 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");
    我会试试这个
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));
    using (StreamReader sr = new StreamReader("http://www.google.com/ig/api?weather=vilnius&hl=ru", true))
{
    XDocument xdoc = XDocument.Load(sr);
}
问题在于编码。如果您使用 StreamReader,它应该检测响应的编码,然后允许您调用 XDocument.Load。