public ActionResult Xml() { return View(); } /// <summary> /// 显示 /// </summary> /// <param name="m"></param> /// <returns></returns> [HttpGet] public string XmlShi(Model m) { HttpWebRequest request = WebRequest.Create("http://flash.weather.com.cn/wmaps/xml/china.xml?spm=a2c4e.10696291.0.0.5c5019a4nz9oyZ&file=china.xml") as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); var xmlStr = reader.ReadToEnd(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlStr); string json = JsonConvert.SerializeXmlNode(doc["china"]); return json; } } /// <summary> /// 添加 /// </summary> /// <param name="m"></param> /// <returns></returns> [HttpPost] public int XmlAdd(Model m) { string sql = $"insert into XMLTable values('{m.quName}','{m.pyName}','{m.cityname}','{m.state1}','{m.state2}','{m.stateDetailed}','{m.tem1}','{m.tem2}','{m.windState}')"; SqlConnection connection = SqlConnectionFactory.Connection; return connection.Execute(sql); }
@{ ViewBag.Title = "Xml"; } <h2>Xml</h2> <!DOCTYPE html> <html> <head> <title>JSONP——XML</title> <script src="jq.js"></script> </head> <body> <table class="table"> <thead> <tr> <td>省</td> <td>英文</td> <td>市</td> <td>未知列1</td> <td>未知列2</td> <td>天气</td> <td>未知列3</td> <td>未知列4</td> <td>状态</td> </tr> </thead> <tbody id="tb"></tbody> </table> </body> </html> <script src="~/Scripts/jquery-3.3.1.js"></script> <script> $(function () { $.ajax({ url: "/Home/XmlShi", dataType: "json", success: function (d) { for (var i = 0; i < d.china.city.length; i++) { var tr = "<tr>" + "<td>" + d.china.city[i]["@@quName"] + "</td>" + "<td>" + d.china.city[i]["@@pyName"] + "</td>" + "<td>" + d.china.city[i]["@@cityname"] + "</td>" + "<td>" + d.china.city[i]["@@state1"] + "</td>" + "<td>" + d.china.city[i]["@@state2"] + "</td>" + "<td>" + d.china.city[i]["@@stateDetailed"] + "</td>" + "<td>" + d.china.city[i]["@@tem1"] + "</td>" + "<td>" + d.china.city[i]["@@tem2"] + "</td>" + "<td>" + d.china.city[i]["@@windState"] + "</td>" + "</tr>"; $("#tb").append(tr); //添加 $.ajax({ url: "/Home/XmlAdd", type: "post", data: { quName: d.china.city[i]["@@quName"], pyName: d.china.city[i]["@@pyName"], cityname: d.china.city[i]["@@cityname"], state1: d.china.city[i]["@@state1"], state2: d.china.city[i]["@@state2"], stateDetailed: d.china.city[i]["@@stateDetailed"], tem1: d.china.city[i]["@@tem1"], tem2: d.china.city[i]["@@tem2"], windState: d.china.city[i]["@@windState"] }, success: function (d) {} }) } } }) }) </script>