1 public string GetWeather()
2 {
3 string weatherXML = GetRequestData("http://flash.weather.com.cn/wmaps/xml/china.xml?spm=a2c4e.10696291.0.0.5c5019a4nz9oyZ&file=china.xml");
4 XmlDocument xml = new XmlDocument();
5 xml.LoadXml(weatherXML);
6 XmlNode root = xml.SelectSingleNode("china");
7 var list = Newtonsoft.Json.JsonConvert.SerializeXmlNode(root);
8 XmlNodeList childlist = root.ChildNodes;
9 return list;
10 }
11
12 public static string GetRequestData(string sUrl)
13 {
14 //使用HttpWebRequest类的Create方法创建一个请求到uri的对象。
15 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sUrl);
16 //指定请求的方式为Get方式
17 request.Method = WebRequestMethods.Http.Get;
18 //获取该请求所响应回来的资源,并强转为HttpWebResponse响应对象
19 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
20 //获取该响应对象的可读流
21 StreamReader reader = new StreamReader(response.GetResponseStream());
22 //将流文本读取完成并赋值给str
23 string str = reader.ReadToEnd();
24 //关闭响应
25 response.Close();
26 return str;
27 }
/// <summary>
/// 解析xml接口 返回json对象
/// </summary>
/// <param name="m"></param>
/// <param name="url">接口路径</param>
/// <returns></returns>
[HttpGet]
public string XmlShi(Model m,string url)
{
HttpWebRequest request = WebRequest.Create("http://flash.weather.com.cn/wmaps/xml/china.xmlspm=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;
}
}
///前台页面
@{
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>