• 一个面试题(机试),关于XML的AJAX的读取与删除


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

    View Code
    <?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();
                    }
    千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs
  • 相关阅读:
    运维IT必备程序安装包
    网络基础TCP三次握手四次挥手
    新建Weblogic域启动报BEA090403和BEA000386提示密码认证有问题
    Weblogic开发模式和生产产品模式互换
    web api 返回 去除双引号转义符
    freeswitch esl :Rejected by acl “loopback.auto“问题
    freeswitch SIP 服务器一些常用配置
    因果推断综述
    Django 项目配置拆分独立
    wayne编译支持k8s1.16+
  • 原文地址:https://www.cnblogs.com/kingkoo/p/2578325.html
Copyright © 2020-2023  润新知