博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取接口数据(xml格式)转为json格式
阅读量:6111 次
发布时间:2019-06-21

本文共 2910 字,大约阅读时间需要 9 分钟。

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;namespace WebApplication1{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            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.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;        }    }}

  

转载于:https://www.cnblogs.com/lgx5/p/10167655.html

你可能感兴趣的文章
Web前端开发十日谈
查看>>
关于jsp页面乱码写得好的一篇文章
查看>>
Linux 基础知识
查看>>
写了一个采集的类,个人感觉不错,代码普通,但灵活性高
查看>>
collector v1.02采集核心代码版本升级中
查看>>
ddns动态域名解析系统
查看>>
Spring Data Redis 2 之消息订阅
查看>>
luov之SMTP报错详解
查看>>
软件概要设计做什么,怎么做
查看>>
dwr
查看>>
java的特殊符号
查看>>
word2010中去掉红色波浪线的方法
查看>>
fabric上下文管理器(context mangers)
查看>>
JQuery-EasyUI Datagrid数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
查看>>
并发和并行的区别
查看>>
软件测试之作业二
查看>>
Java字符串中常见的10个问题
查看>>
html table 知识点
查看>>
struct和union分析实例
查看>>
xml文件的建立和编码[SimpleXMLElement]
查看>>