範例:
https://data.gov.tw/dataset/68186
通常,我們抓到的資料可能是下載實體檔的狀態。
但如果您希望做個 WebService 給別人直接讀取接收回來的字串該怎麼做?
如果,XML檔,有中文字又該怎麼讓它不會有亂碼呢?
/// <summary>
/// 讀取 url 檔案並轉成 String
/// </summary>
/// <param name="url">Open Data url</param>
/// <returns></returns>
public static String ReadXmlFile(String url)
{
var request = WebRequest.Create(url) as HttpWebRequest;
var response = request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
return result;
}
沒有留言:
張貼留言