//控制器代码 /// <summary> /// 页面 /// </summary> /// <returns></returns> public ActionResult Index() { return View(); } /// <summary> /// 显示 /// </summary> /// <returns></returns>//Model是根据你接口所需要的字段定义 [HttpGet] public string XmlShow(XMLTable01 m) { string ConnString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; var url = "是你所需要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 = "自己的sql添加语句"; SqlConnection connection = SqlConnectionFactory.Connection; return connection.Execute(sql); }
//HTML页面@{ 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>
//帮助类 使用了Dapper(也可以不用)
public class SqlConnectionFactory { private static readonly string ConnString = ConfigurationManager.ConnectionStrings["ConStr"].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; } } }
重点:别忘了引用NuGet里的Dapper
Web.config里别忘了连接数据库,写的时候别忘了自己所要创建的Model 如果是死数据可以自己在控制器写,