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;
        }
 
    }
}

  

posted @   每天进步多一点  阅读(1980)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示