C# 獲取接口數據(xml格式)轉爲json格式

流程:XmlDocument-->XmlNode -->XmlNodeList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.IO;

namespace juheWeather
{
    public partial class BJweather : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string callback = Request.QueryString["callback"];
            Response.Write(getWeather());
            Response.End();
        }

        public string getWeather()
        {
            string weatherXML = GetRequestData("http://flash.weather.com.cn/wmaps/xml/beijing.xml");
            XmlDocument xml = new XmlDocument();(深入研究獲取xml各種位置的信息,請百度:XmlDocument,還可以參見readXML實現程序
            xml.LoadXml(weatherXML);
            XmlNode root = xml.SelectSingleNode("beijing");
            XmlNodeList childlist = root.ChildNodes;
            string strResult = "[";
            for (int i = 0; i < childlist.Count; i++)
            {
                strResult += "{'cityname':'" + childlist[i].Attributes["cityname"].Value + "',";
                strResult += "'state1':'" + childlist[i].Attributes["state1"].Value + "',";
                strResult += "'state2':'" + childlist[i].Attributes["state2"].Value + "',";
                strResult += "'stateDetailed':'" + childlist[i].Attributes["stateDetailed"].Value + "',";
                strResult += "'tem1':'" + childlist[i].Attributes["tem1"].Value + "',";
                strResult += "'tem2':'" + childlist[i].Attributes["tem2"].Value + "',";
                strResult += "'temNow':'" + childlist[i].Attributes["temNow"].Value + "',";
                strResult += "'windState':'" + childlist[i].Attributes["windState"].Value + "',";
                strResult += "'windDir':'" + childlist[i].Attributes["windDir"].Value + "',";
                strResult += "'windPower':'" + childlist[i].Attributes["windPower"].Value + "',"; 
                strResult += "'humidity':'" + childlist[i].Attributes["humidity"].Value + "',";
                strResult += "'time':'" + childlist[i].Attributes["time"].Value + "',";
                strResult += "'url':'" + childlist[i].Attributes["url"].Value + "'},";
            }
            strResult = strResult.Substring(0, strResult.Length - 1);
            return strResult + "]";
        }

        public static string GetRequestData(string sUrl)
        {
            //使用HttpWebRequest類的Create方法創建一個請求到uri的對象。
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sUrl);
            //指定請求的方式爲Get方式
            request.Method = WebRequestMethods.Http.Get;
            //獲取該請求所響應回來的資源,並強轉爲HttpWebResponse響應對象
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //獲取該響應對象的可讀流
            StreamReader reader = new StreamReader(response.GetResponseStream());
            //將流文本讀取完成並賦值給str
            string str = reader.ReadToEnd();
            //關閉響應
            response.Close();
            return str;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章