zoukankan      html  css  js  c++  java
  • XML 操作(判断用户登录)

        private string usernames = "";
        private string passwords = "";
        private XmlDocument XmlDoc;
        protected void Page_Load(object sender, EventArgs e)
        {
            usernames = Request.QueryString["username"];
            passwords = Request.QueryString["pwd"];
            if (!IsPostBack)
            {
                loadXml();
                XmlNodeList nodeList = XmlDoc.SelectSingleNode("Users").ChildNodes;
                foreach (XmlNode xn in nodeList)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.HasChildNodes)
                    {
                        string username = "";
                        string pwd = "";
                        foreach (XmlNode xx in xe.ChildNodes)
                        {
                            XmlElement ee = (XmlElement)xx;
                            if (ee.Name == "username")
                            {
                                username = ee.InnerText;
                            }
                            if (ee.Name == "password")
                            {
                                pwd = ee.InnerText;
                            }
                        }
                        if (username == usernames)
                        {
                            if (xe.HasAttribute("Count") && xe.GetAttributeNode("Count").Value == "3"
                                && xe.HasAttribute("LoginDate")
                                && DateTime.Parse(xe.GetAttributeNode("LoginDate").Value).AddMinutes(1) >= DateTime.Now)
                            {
                                Response.Write("请一分钟后再尝试登陆");
                            }
                            if (pwd == passwords)
                            {
                                xe.RemoveAttribute("Count");
                                xe.RemoveAttribute("LoginDate");
                                XmlDoc.Save(Server.MapPath("User.xml"));
                                //Response.Redirect("Default2.aspx");
                                Response.Write("登陆成功");
                            }
                            else
                            {
                                if (xe.HasAttribute("Count"))
                                {
                                    if (Convert.ToInt32(xe.Attributes["Count"].InnerText) <= 2)
                                        xe.Attributes["Count"].InnerText = (Convert.ToInt32(xe.Attributes["Count"].InnerText) + 1).ToString();
                                    else if (Convert.ToInt32(xe.Attributes["Count"].InnerText) > 3)
                                        xe.Attributes["Count"].InnerText = "1";

                                }
                                else
                                {
                                    xe.SetAttribute("Count", "1");
                                }
                                if (!xe.HasAttribute("LoginDate"))
                                {
                                    xe.SetAttribute("LoginDate", DateTime.Now.ToString());
                                }
                                else if (DateTime.Parse(xe.GetAttributeNode("LoginDate").Value).AddMinutes(1) < DateTime.Now)
                                {
                                    xe.RemoveAttribute("Count");
                                    xe.RemoveAttribute("LoginDate");
                                }
                                XmlDoc.Save(Server.MapPath("User.xml"));
                            }
                        }
                    }
                }
            }
        }

        public void loadXml()
        {
            XmlDoc = new XmlDocument();
            XmlDoc.Load(Server.MapPath("User.xml"));
        }

  • 相关阅读:
    投稿007期|令人震惊到发指的PyObject对象代码设计之美
    使用OpenCV通过摄像头捕获实时视频并探测人脸
    洛谷 P1259【黑白棋子的移动】
    入门OJ 1281【营救(save)】
    入门OJ 3204【射击】
    POJ 3126【长度为素数的路径个数】
    POJ 1980【Unit Fraction Partition】
    洛谷 P2374【搬运工】
    【常用算法总结——记忆化搜索】
    P3052 [USACO12MAR]【摩天大楼里的奶牛(Cows in a Skyscraper)】
  • 原文地址:https://www.cnblogs.com/1011004519/p/2154690.html
Copyright © 2011-2022 走看看