C # parsing Xml Interface

 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 is          public  static  String GetRequestData ( String sUrl)
 13 is          {
 14              // use HttpWebRequest Create method creates an object class to a request uri. 
15              the HttpWebRequest Request = (the HttpWebRequest) HttpWebRequest.Create (sUrl);
 16              // embodiment Get request is specified manner 
. 17              request.method = WebRequestMethods.Http.Get;
 18 is              // Get the resource request response back, and strong rotation is HttpWebResponse response object 
. 19              HttpWebResponse response =(The HttpWebResponse) request.GetResponse ();
 20 is              // Get the response object-readable stream 
21 is              the StreamReader Reader = new new the StreamReader (response.GetResponseStream ());
 22 is              // stream read complete text and assigned to STR 
23 is              String STR = reader.ReadToEnd ();
 24              // off response 
25              response.Close ();
 26 is              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>Unknown column. 3 </ TD> 
            </ TR>
                <TD> Status </ TD>
                <TD> Unknown column. 4 </ TD>
        </ 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>  
 
  

  

 

 

 

Guess you like

Origin www.cnblogs.com/wmm0105/p/11764493.html