XML导入数据库

ε祈祈猫儿з 提交于 2019-12-02 18:10:16
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>  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!