C# 获取接口数据(xml格式)转为json格式
2016-11-20 12:50
489 查看
流程: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各种位置的信息,请百度:XmlDo
c40a
cument,还可以参见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;
}
}
}
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各种位置的信息,请百度:XmlDo
c40a
cument,还可以参见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;
}
}
}
相关文章推荐
- Json格式获取里面某个字段的数据获取天气接口
- C#根据指定url下载文件并读取.csv数据转为json格式
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- 我们常常会从网络获取数据,而数据通常又为JSON格式,JSON数据转为Model对象
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- c#中将【对象转成 JSON 格式的字符串】 及获取Json格式字符串并将转成Json对象 及对js数组取数据
- html中通过js获取接口JSON格式数据解析以及跨域问题
- C# 语言如何获取json格式的数据,不用javascript用c#实现。。。
- html中通过js获取接口JSON格式数据解析以及跨域问题
- C# 把数据集生成为JSON格式 并在使用Jquery 获取JSON数据
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android调用接口,获取并解析数据(json格式)
- c# 数据源转Json格式_应用到IPHONE数据服务后台
- C#将datatable生成easyui的绑定tree 的json数据格式
- C#中任意类型数据转成JSON格式数据输出
- [原] 一种将数据表DataTable转为JSON格式的新方法
- 用C#实现多叉树的生成并转化为extjs(基于js脚本的ajax UI框架)的TreeNode的json数据格式(格式为Id,Pid,Text)
- 【原创】c#讲数据库数据转换为json格式(使用extjs,easyui中的datagrid)
- DataTable转为JSON数据格式代码