1

ASP.Net ページで天気 API を使用しています。

クエリに言語 (hl) を追加すると、「指定されたエンコーディングでは無効な文字です。1 行目、位置 526」というエラーが表示されます。
言語の get パラメータがなくても機能しますが、出力をローカライズしたいと考えています。

2行目にエラーがある私のコードは次のとおりです。

  XmlDocument doc = new XmlDocument();
            doc.Load("http://www.google.com/ig/api?hl=de&weather=" + location );

これは機能します:

  XmlDocument doc = new XmlDocument();
            doc.Load("http://www.google.com/ig/api?weather=" + location );

何か案が?

4

2 に答える 2

3

何らかの理由で、Google は出力を UTF エンコードしていません。補償する方法は次のとおりです。

WebClient client = new WebClient();
string data = client.DownloadString("http://www.google.com/ig/api?hl=de&weather=YourTown");

byte[] encoded = Encoding.UTF8.GetBytes(data);

MemoryStream stream = new MemoryStream(encoded);

XmlDocument xml = new XmlDocument();
xml.Load(stream);

Console.WriteLine(xml.InnerXml);
Console.ReadLine();
于 2011-04-04T21:12:09.713 に答える
2

以下のようHttpWebRequestに代わりに使用して実行できます。WebClient

HttpWebRequest myRequest;  
HttpWebResponse myResponse= null;  
XmlDocument MyXMLdoc = null; 

myRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api" + 
    "?weather=" + string.Format(location));  
myResponse = (HttpWebResponse)myRequest.GetResponse();  
MyXMLdoc = new XmlDocument();  
MyXMLdoc.Load(myResponse.GetResponseStream());  
于 2011-06-29T12:12:07.133 に答える