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


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

    复制代码
    <?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>
    复制代码


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

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

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

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

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

    复制代码
    <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>
    复制代码

    后台删除:

    复制代码
    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();
                    }
    复制代码

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

  • 相关阅读:
    java中使用MD5加密的算法
    [转]自定义注释@interface的用法
    [转]Spring IOC详解
    [转]sed命令详解
    SimpleDateFormat线程不安全问题处理
    Unable to locate the Javac Compiler 解决办法
    MySQL ERROR 1045错误解决办法
    Python装饰器学习(九步入门)
    python 可变参数函数定义* args和**kwargs的用法
    如何更好的利用Node.js的性能极限
  • 原文地址:https://www.cnblogs.com/jsonzheng/p/3761000.html
Copyright © 2020-2023  润新知