xml文件的增删改读
Posted 576
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml文件的增删改读相关的知识,希望对你有一定的参考价值。
最近学习了利用XmlDocument对象对xml进行增删改读操作,就写了一个小的例子记录下来,加深印象,以后忘了也可以找出来看看。
xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <Users> 3 <User Id="1"> 4 <Name>吴奇隆</Name> 5 <Sex>1</Sex> 6 <Phone>888888</Phone> 7 </User> 8 <User Id="2"> 9 <Name>刘诗诗</Name> 10 <Sex>0</Sex> 11 <Phone>666999</Phone> 12 </User> 13 <User Id="3"> 14 <Name>刘德华</Name> 15 <Sex>1</Sex> 16 <Phone>999999</Phone> 17 </User> 18 <User Id="4"> 19 <Name>王祖贤</Name> 20 <Sex>0</Sex> 21 <Phone>888899</Phone> 22 </User> 23 <User Id="5"> 24 <Name>吴倩莲</Name> 25 <Sex>0</Sex> 26 <Phone>888999</Phone> 27 </User> 28 <User Id="6"> 29 <Name>张卫健</Name> 30 <Sex>1</Sex> 31 <Phone>666888</Phone> 32 </User> 33 <User Id="7"> 34 <Name>关之琳</Name> 35 <Sex>0</Sex> 36 <Phone>888666</Phone> 37 </User> 38 <User Id="8"> 39 <Name>张敏</Name> 40 <Sex>0</Sex> 41 <Phone>888866</Phone> 42 </User> 43 <User Id="9"> 44 <Name>梁朝伟</Name> 45 <Sex>1</Sex> 46 <Phone>888889</Phone> 47 </User> 48 <User Id="10"> 49 <Name>李连杰</Name> 50 <Sex>1</Sex> 51 <Phone>888886</Phone> 52 </User> 53 <User Id="11"> 54 <Name>袁洁莹</Name> 55 <Sex>0</Sex> 56 <Phone>666999</Phone> 57 </User> 58 </Users>
xml文件对应的类:
1 public class User 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public int Sex { get; set; } 6 public string Phone { get; set; } 7 8 }
前台页面用的是Bootstrap Table
前台代码:
1 <!DOCTYPE html> 2 3 <html> 4 <head> 5 <meta name="viewport" content="width=device-width" /> 6 <title>Index</title> 7 <link href="~/scripts/bootstrap-table/css/bootstrap.css" rel="stylesheet" /> 8 <link href="~/scripts/bootstrap-table/css/bootstrap-table.css" rel="stylesheet" /> 9 <script src="~/scripts/bootstrap-table/js/jquery-1.10.2.js"></script> 10 <script src="~/scripts/bootstrap-table/js/bootstrap.js"></script> 11 <script src="~/scripts/bootstrap-table/js/bootstrap-table.js"></script> 12 <script src="~/scripts/bootstrap-table/js/bootstrap-table-zh-CN.js"></script> 13 <script type="text/javascript"> 14 $(function () { 15 $("#table").bootstrapTable({ 16 toolbar: \'#toolbar\',//工具栏 17 pagination: true,//是否显示分页条 18 pageNumber: 1,//首页页码 19 pageSize: 10,//每页条数 20 pageList: [10, 20, 30],//可供选择的页面数据条数 21 url: \'/UserManager/GetUsers\',//获取数据的url 22 columns: [ 23 { 24 checkbox: true//设置复选框 25 }, 26 { 27 field: \'Id\', 28 title: \'编号\' 29 }, 30 { 31 field: \'Name\', 32 title: \'姓名\' 33 }, { 34 field: \'Sex\', 35 title: \'性别\', 36 formatter: function (value, row, index) { 37 if (value == "1") { 38 return \'男\'; 39 } else { 40 return \'女\'; 41 } 42 } 43 }, { 44 field: \'Phone\', 45 title: \'电话\' 46 }, { 47 field: \'operate\', 48 title: \'操作\', 49 formatter: function (value,row,index) { 50 return \'<a href="javascript:void(0)" onclick="deleteUser(\' + row.Id + \')">删除</a>\' 51 } 52 }] 53 }); 54 }); 55 //删除 56 function deleteUser(id) { 57 if (confirm("你确定要删除吗?")) { 58 $.get("/UserManager/DeleteUser?Id=" + id, null, function (data) { 59 if (data == "ok") { 60 $("#table").bootstrapTable(\'refresh\'); 61 } else { 62 alert("删除失败!"); 63 } 64 }) 65 } 66 } 67 //添加 68 function Add() { 69 var id = $("#uid").val(); 70 var name = $("#name").val(); 71 var sex = $("#sex").val(); 72 var phone = $("#phone").val(); 73 $.post("/UserManager/AddUser", {Id:id, Name: name, Sex: sex, Phone: phone }, function (data) { 74 if (data == "ok") { 75 $(\'#addModal\').modal(\'hide\'); 76 $("#table").bootstrapTable(\'refresh\'); 77 $("#uid").val(""); 78 $("#name").val(""); 79 $("#sex").val(""); 80 $("#phone").val(""); 81 } else { 82 alert("添加失败!"); 83 } 84 }); 85 } 86 //修改 87 function Edit() { 88 var id = $("#editId").val(); 89 var name=$("#editName").val(); 90 var sex= $("#editSex").val(); 91 var phone = $("#editPhone").val(); 92 $.post("/UserManager/EditUser", { Id: id, Name: name, Sex: sex, Phone: phone }, function (data) { 93 if (data == "ok") { 94 $(\'#editModal\').modal(xml的增删改