码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET中关于XML的AJAX的读取与删除

时间:2014-05-31 16:42:40      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

一个XML文件,名称就暂定为GroupStudents.xml吧,内容如下:

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<GroupStutents>
  <GroupStutent value="个人资料">
    <Item>
      <ID>0011-0220-302</ID>
      <Name>名称01</Name>
      <StartTime>1900-01-01</StartTime>
      <EndTime>2012-02-02</EndTime>
      <Description>身份证信息0000</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
    <Item>
      <ID>0011-0220-303</ID>
      <Name>名称02</Name>
      <StartTime>1900-01-01</StartTime>
      <EndTime>2012-02-02</EndTime>
      <Description>身份证信息0000</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
    <Item>
      <ID>0011-0220-304</ID>
      <Name>名称03</Name>
      <StartTime>1900-01-01</StartTime>
      <EndTime>2012-02-02</EndTime>
      <Description>身份证信息0000</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
  </GroupStutent>
  <GroupStutent value="企业资料">
    <Item>
      <ID>0020-0220-303</ID>
      <Name>企业05</Name>
      <StartTime>1900-12-01</StartTime>
      <EndTime>2012-02-02</EndTime>
      <Description>身份证信息0000</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
    <Item>
      <ID>0020-0220-304</ID>
      <Name>企业98</Name>
      <StartTime>1900-01-01</StartTime>
      <EndTime>2012-12-02</EndTime>
      <Description>身份证信息0001</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
  </GroupStutent>
  <GroupStutent value="其它资料">
    <Item>
      <ID>0030-0220-301</ID>
      <Name>其它wew</Name>
      <StartTime>2012-01-01</StartTime>
      <EndTime>2012-02-12</EndTime>
      <Description>身份证信息00200</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
    <Item>
      <ID>0030-0220-303</ID>
      <Name>其它322</Name>
      <StartTime>1990-01-12</StartTime>
      <EndTime>2012-02-02</EndTime>
      <Description>身份证信息00枯00</Description>
      <Remark>0011-0220-302</Remark>
    </Item>
  </GroupStutent>
</GroupStutents>
bubuko.com,布布扣
bubuko.com,布布扣


要求做成下面的样子,不能在网络上查资料~看看自己的动手能力

bubuko.com,布布扣

要求使用jquery,ajax读取XML并能删除(ajax)数据.并保存XML.

删除后台保存可以用到后台代码.

 

 

亲们,动手吧..看你能多久搞定

 

最后,贴上自己写的代码吧.

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
<form id="form1" runat="server">
    <div>
        <table id=‘tabList‘ cellpadding="0" border="1" cellspacing="0" style="border-collapse: collapse">
            <thead>
                <tr>
                    <th>
                        Name
                    </th>
                    <th>
                        FROM
                    </th>
                    <th>
                        TO
                    </th>
                    <th>
                        Remark
                    </th>
                    <th>
                        Description
                    </th>
                    <th>
                        操作
                    </th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    </form>

    <script type="text/javascript">
        $(function() {
            var tbody = $(#tabList tbody);
            $.ajax({
                url: GroupStudents.xml?r= + Math.random(),
                dataType: xml,
                success: function(result) {
                    $(result).find(GroupStutent).each(function() {
                        var str = ($(this).attr(value));
                        tbody.append(<tr><td colspan="6" class="title"> + str + </td></tr>);
                        $(this).find(Item).each(function() {
                            var html = "<tr>";
                            html += "<td>" + $(this).find(Name).text() + "</td>";
                            html += "<td>" + $(this).find(StartTime).text() + "</td>";
                            html += "<td>" + $(this).find(EndTime).text() + "</td>";
                            html += "<td>" + $(this).find(Remark).text() + "</td>";
                            html += "<td>" + $(this).find(Description).text() + "</td>";
                            html += "<td><input type=‘button‘ class=‘btnDelete‘ rel=‘" + $(this).find(ID).text() + "‘ value=‘删除‘></td>";
                            html += "</tr>";
                            tbody.append(html);
                        });
                    });

                    $(#tabList tbody .btnDelete).bind(click, function() {
                        if (!confirm(Are your sure delete?)) return;
                        var button = $(this);
                        $.ajax({
                            url: XMLReader.aspx?action=delete&id= + $(this).attr(rel) + &r= + Math.random(),
                            method: POST,
                            success: function(res) {
                                if (res == "ok") {
                                    button.parents(tr).remove();
                                    alert(删除成功);
                                } else {
                                    alert(res);
                                }
                            }
                        });
                    });
                }
            });

        });
    </script>
bubuko.com,布布扣
bubuko.com,布布扣

后台删除:

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
if (Request["action"] == "delete")
                {
                    Response.Clear();
                    try
                    {
                        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

                        xmlDoc.Load(Server.MapPath("/XML/GroupStudents.xml"));
                        //GroupStutents/GroupStutent/Item[@ID=‘" + Request["id"] + "‘] 表示属性,没有@表示节点
                        XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/GroupStutents/GroupStutent/Item[ID=‘" + Request["id"] + "‘]");
                        if (nodeList.Count > 0)
                        {
                            foreach (XmlNode node in nodeList)
                            {
                                node.ParentNode.RemoveChild(node);
                            }
                            xmlDoc.Save(Server.MapPath("/XML/GroupStudents.xml"));
                            Response.Write("ok");
                        }
                        else
                        {
                            Response.Write("error");
                        }

                    }
                    catch
                    {
                        Response.Write("fail");
                    }
                    Response.End();
                }
bubuko.com,布布扣
bubuko.com,布布扣

 

来源:http://www.cnblogs.com/kingkoo/archive/2012/07/05/2578325.html

 

ASP.NET中关于XML的AJAX的读取与删除,布布扣,bubuko.com

ASP.NET中关于XML的AJAX的读取与删除

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/jsonzheng/p/3761000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!