XML导入数据库

 

 

 

<!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);

}
}
})
})
</script>

 

public ActionResult Xml()
{
return View();
}
[HttpGet]
public string XmlShi(Model m)
{
string ConnString = ConfigurationManager.ConnectionStrings["DapperDemo"].ConnectionString;
var url = "http://flash.weather.com.cn/wmaps/xml/china.xml?spm=a2c4e.10696291.0.0.5c5019a4nz9oyZ&file=china.xml";
XmlDocument doc = new XmlDocument();//创建XML操作对象
            doc.Load(@url);//加载xml文件的路径
            //doc.DocumentElement 是获取XML的根节点
            XmlNodeList UserNodes = doc.DocumentElement.ChildNodes;//获取根节点下的子节点,注意是集合,所以返回的是所有子节点
            foreach (var item in UserNodes)
{
Model m1 = new Model();
                //读取每一个子节点下面的属性值
                XmlElement userEle = (XmlElement)item;//将类型强制转换为XmlElement
                m1.quName = userEle.GetAttribute("quName");
//XmlNode userNameNode = userEle.SelectSingleNode("UserName");
//uname = userNameNode.InnerText;
m1.pyName = userEle.GetAttribute("pyName");
m1.cityname = userEle.GetAttribute("cityname");
m1.state1 = userEle.GetAttribute("state1");
m1.state2 = userEle.GetAttribute("state2");
m1.stateDetailed = userEle.GetAttribute("stateDetailed");
m1.tem1 = userEle.GetAttribute("tem1");
m1.tem2 = userEle.GetAttribute("tem2");
m1.windState = userEle.GetAttribute("windState");
XmlAdd(m1);
}
string json = JsonConvert.SerializeXmlNode(doc["china"]);
return json;
}
[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);
}

posted on 2019-10-29 09:24  林深鹿  阅读(790)  评论(1编辑  收藏  举报

导航