C# 获取接口数据(xml格式)转为json格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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; } } } |
分类:
Xml
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战