XML导入数据库
/// <summary>
/// 页面
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View();
}
/// <summary>
/// 显示
/// </summary>
/// <returns></returns>
[HttpGet]
public string XmlShow(XMLTable01 m)
{
string ConnString = ConfigurationManager.ConnectionStrings["ConStr"].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)
{
XMLTable01 m1 = new XMLTable01();
//读取每一个子节点下面的属性值
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;
}
/// <summary>
/// xml添加数据库
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
[HttpPost]
public int XmlAdd(XMLTable01 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);
}
@{
Layout = null;
}
<!DOCTYPE html>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
$(function () {
$.ajax({
url: "/Home/XmlShow",
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>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<td>quName</td>
<td>pyName</td>
<td>cityname</td>
<td>state1</td>
<td>state2</td>
<td>stateDetailed</td>
<td>tem1</td>
<td>tem2</td>
<td>windState</td>
</tr>
</thead>
<tbody id="tb"></tbody>
</table>
</div>
</body>
</html>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
$(function () {
$.ajax({
url: "/Home/XmlShow",
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>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<td>quName</td>
<td>pyName</td>
<td>cityname</td>
<td>state1</td>
<td>state2</td>
<td>stateDetailed</td>
<td>tem1</td>
<td>tem2</td>
<td>windState</td>
</tr>
</thead>
<tbody id="tb"></tbody>
</table>
</div>
</body>
</html>
public class SqlConnectionFactory { private static readonly string ConnString = ConfigurationManager.ConnectionStrings["DapperDemo"].ConnectionString; private static object _obj = new object(); public static SqlConnection Connection { get { SqlConnection connection = null; if (connection == null) { lock (_obj) { if (connection == null) { connection = new SqlConnection(ConnString); } } } connection.Open(); return connection; } } }
引用dapper